]> granicus.if.org Git - esp-idf/commitdiff
cmake: Move global IDF-specific compiler/linker options out of the toolchain file
authorAngus Gratton <angus@espressif.com>
Tue, 16 Jan 2018 00:16:14 +0000 (11:16 +1100)
committerAngus Gratton <gus@projectgus.com>
Sun, 29 Apr 2018 23:59:20 +0000 (09:59 +1000)
Should restore compatibility with cmake pre-v3.7

idf.cmake
toolchain.cmake
tools/cmake/idf_functions.cmake [new file with mode: 0644]
tools/cmake/utilities.cmake

index 225b229e0e9dd123f915a1330b6f50289067ef38..c6cb99d7ae15a74e50add168cde68beb78fc721a 100644 (file)
--- a/idf.cmake
+++ b/idf.cmake
@@ -34,6 +34,7 @@ include(components)
 include(kconfig)
 include(crosstool_version_check)
 include(git_submodules)
+include(idf_functions)
 
 #
 # Warn if the toolchain version doesn't match
@@ -61,9 +62,7 @@ include(${SDKCONFIG_CMAKE})
 
 #
 # Add some idf-wide definitions
-#
-add_definitions(-DESP_PLATFORM)
-add_definitions(-DHAVE_CONFIG_H)
+idf_set_global_compiler_options()
 
 git_describe(GIT_REVISION)
 add_definitions(-DIDF_VER=\"${GIT_REVISION}\")
index 12e3f5a5a8836ecd5a3294b2320e8907df4a9f17..cecba78af417271a04a0b7ea031c1032f195be7c 100644 (file)
@@ -4,8 +4,4 @@ set(CMAKE_C_COMPILER xtensa-esp32-elf-gcc)
 set(CMAKE_CXX_COMPILER xtensa-esp32-elf-g++)\r
 set(CMAKE_ASM_COMPILER xtensa-esp32-elf-gcc)\r
 \r
-set(CMAKE_C_FLAGS_INIT "-Og -ggdb -std=gnu99 -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -Wno-old-style-declaration")\r
-set(CMAKE_CXX_FLAGS_INIT "-Og -ggdb -std=gnu++11 -fno-exceptions -fno-rtti -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare")\r
-# TODO work out a better way to pass start-group...\r
-# TODO put -Map arg somewhere more logical, make it depend on the app name\r
-set(CMAKE_EXE_LINKER_FLAGS_INIT ${CMAKE_EXE_LINKER_FLAGS} "-nostdlib -Wl,--gc-sections -Wl,--cref -Wl,--Map=linker.map -Wl,--start-group" CACHE STRING "Linker Flags")\r
+set(CMAKE_EXE_LINKER_FLAGS "-nostdlib" CACHE STRING "Linker Base Flags")\r
diff --git a/tools/cmake/idf_functions.cmake b/tools/cmake/idf_functions.cmake
new file mode 100644 (file)
index 0000000..72216df
--- /dev/null
@@ -0,0 +1,67 @@
+# Some IDF-specific functions and functions
+
+
+# Add all the IDF global compiler & preprocessor options
+# (applied to all components). Some are config-dependent
+#
+# If you only want to set options for a particular component,
+# don't call or edit this function. TODO DESCRIBE WHAT TO DO INSTEAD
+#
+function(idf_set_global_compiler_options)
+  add_definitions(-DESP_PLATFORM)
+  add_definitions(-DHAVE_CONFIG_H)
+
+  if(CONFIG_OPTIMIZATION_LEVEL_RELEASE)
+    add_compile_options(-Os)
+  else()
+    add_compile_options(-Og)
+  endif()
+
+  add_c_compile_options(-std=gnu99)
+
+  add_cxx_compile_options(-std=gnu++11 -fno-rtti)
+
+  if(CONFIG_CXX_EXCEPTIONS)
+    add_cxx_compile_options(-fexceptions)
+  else()
+    add_cxx_compile_options(-fno-exceptions)
+  endif()
+
+  # Default compiler configuration
+  add_compile_options(-ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib)
+
+  # Default warnings configuration
+  add_compile_options(-Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare)
+  add_c_compile_options(-Wno-old-style-declaration)
+
+  # Stack protection
+  if(NOT BOOTLOADER_BUILD)
+    if(CONFIG_STACK_CHECK_NORM)
+      add_compile_options(-fstack-protector)
+    elseif(CONFIG_STACK_CHECK_STRONG)
+      add_compile_options(-fstack-protector-strong)
+    elseif(CONFIG_STACK_CHECK_ALL)
+      add_compile_options(-fstack-protector-all)
+    endif()
+  endif()
+
+  if(CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED)
+    add_definitions(-DNDEBUG)
+  endif()
+
+  # Always generate debug symbols (even in Release mode, these don't
+  # go itno ther final binary
+  add_compile_options(-ggdb)
+
+endfunction()
+
+# Override add_executable to add IDF-specific
+# linker flags & map file to all built executables
+function(add_executable target)
+  get_filename_component(basename ${target} NAME_WE)
+  set(mapfile "${basename}.map")
+
+  _add_executable(${ARGV})
+
+  target_link_libraries(${target} "-Wl,--gc-sections -Wl,--cref -Wl,--Map=${mapfile} -Wl,--start-group")
+endfunction()
index 962d46c25a4836601853f3f0e5cb5096ac8b757e..ce71f5ced8f2c1abf28c4a79458046be0424bd98 100644 (file)
@@ -72,3 +72,25 @@ function(move_if_different source destination)
   endif()
 
 endfunction()
+
+# add_compile_options variant for C++ code only
+#
+# This adds global options, set target properties for
+# component-specific flags
+function(add_cxx_compile_options)
+  foreach(option ${ARGV})
+    # note: the Visual Studio Generator doesn't support this...
+    add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${option}>)
+  endforeach()
+endfunction()
+
+# add_compile_options variant for C code only
+#
+# This adds global options, set target properties for
+# component-specific flags
+function(add_c_compile_options)
+  foreach(option ${ARGV})
+    # note: the Visual Studio Generator doesn't support this...
+    add_compile_options($<$<COMPILE_LANGUAGE:C>:${option}>)
+  endforeach()
+endfunction()