summaryrefslogtreecommitdiff
path: root/cmake/CheckFortifyLevel.cmake
diff options
authorHao Yao <[email protected]>2024-07-19 10:58:05 +0800
committerGitHub <[email protected]>2024-07-19 10:58:05 +0800
commit8863bda8b15bef415f112700d0fb04e00a48dbee (patch)
tree18a147bd5f4f0476b49c0e8403dcdbbd4310e6dc /cmake/CheckFortifyLevel.cmake
parent289e645dffbd0ea633f10bb4f93855f1e4429e9a (diff)
parenta354967a6a697157a6c0450a33bceed42d053c1e (diff)
Merge pull request #115 from hao-yao/main
ARL platform Beta release on 2024-07-18
Diffstat (limited to 'cmake/CheckFortifyLevel.cmake')
-rw-r--r--cmake/CheckFortifyLevel.cmake42
1 files changed, 42 insertions, 0 deletions
diff --git a/cmake/CheckFortifyLevel.cmake b/cmake/CheckFortifyLevel.cmake
new file mode 100644
index 0000000..a27498c
--- /dev/null
+++ b/cmake/CheckFortifyLevel.cmake
@@ -0,0 +1,42 @@
+# CheckFortifySource.cmake
+function(check_fortify_source output_variable)
+ # Create a simple C++ source file to check _FORTIFY_SOURCE
+ set(CHECK_SOURCE_CODE "#include <stdio.h>
+#ifndef _FORTIFY_SOURCE
+#define _FORTIFY_SOURCE 0
+#endif
+int main(){printf(\"%d\", _FORTIFY_SOURCE);return 0;}")
+
+ # Set the full path for the source file
+ set(SOURCE_FILE_NAME "${CMAKE_BINARY_DIR}/check_fortify_source.c")
+ file(WRITE "${SOURCE_FILE_NAME}" "${CHECK_SOURCE_CODE}")
+
+ # Try to compile the source file
+ try_compile(FORTIFY_SOURCE_COMPILED
+ "${CMAKE_BINARY_DIR}/temp" "${SOURCE_FILE_NAME}"
+ COMPILE_DEFINITIONS "-O2"
+ COPY_FILE "${CMAKE_BINARY_DIR}/check_fortify_source.out"
+ OUTPUT_VARIABLE COMPILE_OUTPUT
+ )
+
+ # Check if compilation was successful
+ if(FORTIFY_SOURCE_COMPILED)
+ # Run the compiled program to get the value of _FORTIFY_SOURCE
+ execute_process(COMMAND "${CMAKE_BINARY_DIR}/check_fortify_source.out"
+ RESULT_VARIABLE RUN_RESULT
+ OUTPUT_VARIABLE FORTIFY_SOURCE_VALUE
+ ERROR_QUIET
+ OUTPUT_STRIP_TRAILING_WHITESPACE)
+ if(RUN_RESULT EQUAL 0)
+ # Set the variable outside the function
+ set(${output_variable} "${FORTIFY_SOURCE_VALUE}" PARENT_SCOPE)
+ else()
+ message(STATUS "Failed to run the compiled test program.")
+ set(${output_variable} "" PARENT_SCOPE)
+ endif()
+ else()
+ message(STATUS "Compilation failed; _FORTIFY_SOURCE is not defined.")
+ message(STATUS "Compiler output: ${COMPILE_OUTPUT}")
+ set(${output_variable} "" PARENT_SCOPE)
+ endif()
+endfunction()