]> granicus.if.org Git - esp-idf/commitdiff
cmake: Refactor main cmake project logic
authorAngus Gratton <angus@espressif.com>
Wed, 17 Jan 2018 03:01:57 +0000 (14:01 +1100)
committerAngus Gratton <gus@projectgus.com>
Sun, 29 Apr 2018 23:59:20 +0000 (09:59 +1000)
components/bootloader/subproject/CMakeLists.txt
examples/get-started/hello_world/CMakeLists.txt
tools/cmake/components.cmake
tools/cmake/idf_functions.cmake
tools/cmake/kconfig.cmake
tools/cmake/project.cmake [moved from idf.cmake with 52% similarity]

index e8f920f2edb9afbe1df6c33983a23df9a3476fca..ab7b5aaa6e74f4886676de8fb49f6934430e3de2 100644 (file)
@@ -12,7 +12,7 @@ if(NOT SDKCONFIG)
   message(FATAL_ERROR "Bootloader subproject expects the SDKCONFIG variable to be passed in by the app build process.")
 endif()
 
-include($ENV{IDF_PATH}/idf.cmake)
+include($ENV{IDF_PATH}/tools/cmake/project.cmake)
 
 add_executable(bootloader.elf
   main/bootloader_start.c
index 62a03947f680d071d5ddb3affba39db12a17f3f7..4e66c5efdb0dc17eb906a4ae86d3485fff878ccb 100644 (file)
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.5)
 set(CMAKE_TOOLCHAIN_FILE $ENV{IDF_PATH}/toolchain.cmake)
 project(idf_project ASM C CXX)
 
-include($ENV{IDF_PATH}/idf.cmake)
+include($ENV{IDF_PATH}/tools/cmake/project.cmake)
 
 add_executable(hello_world.elf
   main/hello_world_main.c)
index 540caadf9b0d1a43c1027932a592987f874c5452..3a9e1fc2f7cb08a4c9f0580056e804d2155c95dc 100644 (file)
@@ -4,7 +4,7 @@
 #
 # component_paths contains only unique component names. Directories
 # earlier in the component_dirs list take precedence.
-function(find_all_components component_dirs filter_names component_paths component_names)
+function(components_find_all component_dirs filter_names component_paths component_names)
   # component_dirs entries can be files or lists of files
   set(paths "")
   set(names "")
@@ -44,7 +44,7 @@ function(find_all_components component_dirs filter_names component_paths compone
 
   set(${component_paths} ${paths} PARENT_SCOPE)
   set(${component_names} ${names} PARENT_SCOPE)
-endfunction()
+endfunction(components_find_all)
 
 
 # Add a component to the build, using the COMPONENT variables defined
@@ -98,16 +98,16 @@ function(register_component)
     target_include_directories(${component} PRIVATE ${include_dir})
   endforeach()
 
-endfunction()
+endfunction(register_component)
 
 function(register_config_only_component)
   get_filename_component(component_dir ${CMAKE_CURRENT_LIST_FILE} DIRECTORY)
   get_filename_component(component ${component_dir} NAME)
 
   # No-op for now...
-endfunction()
+endfunction(register_config_only_component)
 
-function(finish_component_registration)
+function(components_finish_registration)
   # each component should see the include directories of each other
   #
   # (we can't do this until all components are registered, because if(TARGET ...) won't work
@@ -130,4 +130,4 @@ function(finish_component_registration)
 
   # set COMPONENT_LIBRARIES in top-level scope
   set(COMPONENT_LIBRARIES "${COMPONENT_LIBRARIES}" PARENT_SCOPE)
-endfunction()
+endfunction(components_finish_registration)
index 6da9780381fdc4b2270cc945c2bfd634dc9e02a9..34785b102000ae91ed57779cc17e3d71878bc628 100644 (file)
@@ -1,5 +1,24 @@
 # Some IDF-specific functions and functions
 
+include(crosstool_version_check)
+
+macro(idf_set_global_variables)
+
+  set_default(EXTRA_COMPONENT_DIRS "")
+
+  # PROJECT_PATH has the path to the IDF project (top-level cmake directory)
+  #
+  # (cmake calls this CMAKE_SOURCE_DIR, keeping old name for compatibility.)
+  set(PROJECT_PATH "${CMAKE_SOURCE_DIR}")
+
+  # Note: "main" is no longer a component...
+  #
+  set_default(COMPONENT_DIRS "${PROJECT_PATH}/components ${EXTRA_COMPONENT_DIRS} ${IDF_PATH}/components")
+  spaces2list(COMPONENT_DIRS)
+
+  spaces2list(COMPONENTS)
+
+endmacro()
 
 # Add all the IDF global compiler & preprocessor options
 # (applied to all components). Some are config-dependent
@@ -53,7 +72,9 @@ function(idf_set_global_compiler_options)
   # go itno ther final binary
   add_compile_options(-ggdb)
 
-endfunction()
+  add_compile_options("-I${CMAKE_BINARY_DIR}") # for sdkconfig.h
+
+endfunction(idf_set_global_compiler_options)
 
 # Override add_executable to add IDF-specific
 # linker flags & map file to all built executables
@@ -66,4 +87,26 @@ function(add_executable target)
   target_link_libraries(${target} "-Wl,--gc-sections -Wl,--cref -Wl,--Map=${mapfile} -Wl,--start-group")
 
   set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/${mapfile}")
-endfunction()
+endfunction(add_executable)
+
+
+# Verify the IDF environment is configured correctly (environment, toolchain, etc)
+function(idf_verify_environment)
+
+  # Check toolchain is configured properly in cmake
+  if(NOT ( ${CMAKE_SYSTEM_NAME} STREQUAL "Generic" AND ${CMAKE_C_COMPILER} MATCHES xtensa))
+    message(FATAL_ERROR "The parent project CMakeLists.txt file needs to set CMAKE_TOOLCHAIN_FILE "
+      "before including this file. "
+      "Update CMakeLists.txt to match the template project and delete CMakeCache.txt before "
+      "re-running cmake.")
+  endif()
+
+  #
+  # Warn if the toolchain version doesn't match
+  #
+  gcc_version_check("5.2.0")
+  crosstool_version_check("1.22.0-80-g6c4433a")
+
+  
+
+endfunction(idf_verify_environment)
index 6fd68de14527a0b053c39e823ab3783bb03de596..9143776f58ea11af4d4dd0cd83accc3a1ba7d7af 100644 (file)
@@ -1,14 +1,14 @@
 include(ExternalProject)
 
-add_compile_options("-I${CMAKE_BINARY_DIR}")
+macro(kconfig_set_variables)
+  set(MCONF ${IDF_PATH}/tools/kconfig/mconf)
 
-set(MCONF ${IDF_PATH}/tools/kconfig/mconf)
+  set_default(SDKCONFIG ${PROJECT_PATH}/sdkconfig)
+  set(SDKCONFIG_HEADER ${CMAKE_BINARY_DIR}/sdkconfig.h)
+  set(SDKCONFIG_CMAKE ${CMAKE_BINARY_DIR}/sdkconfig.cmake)
 
-set_default(SDKCONFIG ${PROJECT_PATH}/sdkconfig)
-set(SDKCONFIG_HEADER ${CMAKE_BINARY_DIR}/sdkconfig.h)
-set(SDKCONFIG_CMAKE ${CMAKE_BINARY_DIR}/sdkconfig.cmake)
-
-set(ROOT_KCONFIG ${IDF_PATH}/Kconfig)
+  set(ROOT_KCONFIG ${IDF_PATH}/Kconfig)
+endmacro()
 
 # Use the existing Makefile to build mconf when needed
 #
@@ -24,7 +24,7 @@ ExternalProject_Add(mconf
   )
 
 # Find all Kconfig files for all components
-function(build_component_config)
+function(kconfig_process_config)
   file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include/config")
   set(kconfigs )
   set(kconfigs_projbuild )
similarity index 52%
rename from idf.cmake
rename to tools/cmake/project.cmake
index efb4631d2cd3c68282a4d18e0e07069a67393dce..164e923e627c969ee16de4cc643cb3e55ed9b4f7 100644 (file)
--- a/idf.cmake
@@ -2,16 +2,7 @@
 #
 cmake_minimum_required(VERSION 3.5)
 
-#
-# Verify the project configured the environment correctly
-#
-if(NOT ( ${CMAKE_SYSTEM_NAME} STREQUAL "Generic" AND ${CMAKE_C_COMPILER} MATCHES xtensa))
-  message(FATAL_ERROR "The parent project CMakeLists.txt file needs to set CMAKE_TOOLCHAIN_FILE "
-    "before including this file. "
-    "Update CMakeLists.txt to match the template project and delete CMakeCache.txt before "
-    "re-running cmake.")
-endif()
-
+# Set IDF_PATH, as nothing else will work without this
 set(IDF_PATH "$ENV{IDF_PATH}")
 if(NOT IDF_PATH)
   # Documentation says you should set IDF_PATH in your environment, but we
@@ -20,11 +11,6 @@ if(NOT IDF_PATH)
   set($ENV{IDF_PATH} "${IDF_PATH}")
 endif()
 
-# PROJECT_PATH has the path to the IDF project (top-level cmake directory)
-#
-# (cmake calls this CMAKE_SOURCE_DIR, keeping old name for compatibility.)
-set(PROJECT_PATH "${CMAKE_SOURCE_DIR}")
-
 #
 # Load cmake modules
 #
@@ -33,43 +19,34 @@ include(GetGitRevisionDescription)
 include(utilities)
 include(components)
 include(kconfig)
-include(crosstool_version_check)
 include(git_submodules)
 include(idf_functions)
 
-#
-# Warn if the toolchain version doesn't match
-#
-gcc_version_check("5.2.0")
-crosstool_version_check("1.22.0-80-g6c4433a")
-
-#
-# Configure optional variables
-#
-set_default(EXTRA_COMPONENT_DIRS "")
+# Verify the environment is configured correctly
+idf_verify_environment()
 
-# Note: "main" is no longer a component...
-#
-set_default(COMPONENT_DIRS "${PROJECT_PATH}/components ${EXTRA_COMPONENT_DIRS} ${IDF_PATH}/components")
-spaces2list(COMPONENT_DIRS)
+# Set global variables used by rest of the build
+idf_set_global_variables()
 
-spaces2list(COMPONENTS)
 # Search COMPONENT_DIRS for COMPONENTS, make a list of full paths to each component in COMPONENT_PATHS
-find_all_components("${COMPONENT_DIRS}" "${COMPONENTS}" COMPONENT_PATHS COMPONENTS)
-build_component_config()
+components_find_all("${COMPONENT_DIRS}" "${COMPONENTS}" COMPONENT_PATHS COMPONENTS)
+
+kconfig_set_variables()
+
+kconfig_process_config()
 
 # Include sdkconfig.cmake so rest of the build knows the configuration
 include(${SDKCONFIG_CMAKE})
 
-#
 # Add some idf-wide definitions
 idf_set_global_compiler_options()
 
+# Check git version (may trigger reruns of cmake)
+# & set GIT_REVISION/IDF_VER variable
 git_describe(GIT_REVISION)
 add_definitions(-DIDF_VER=\"${GIT_REVISION}\")
 git_submodule_check("${IDF_PATH}")
 
-add_compile_options("-I${CMAKE_BINARY_DIR}") # for sdkconfig.h
 
 #
 # Add components to the build
@@ -79,10 +56,9 @@ foreach(component ${COMPONENT_PATHS})
   add_subdirectory(${component} ${component_name})
 endforeach()
 
-finish_component_registration()
+components_finish_registration()
 
 # Load the targets for the bootloader subproject
 if(NOT BOOTLOADER_BUILD)
   include(bootloader_subproject)
 endif()
-