Update library CMake system, to support automatic creation of options file, if user doesn't provide one

This commit is contained in:
Tilen Majerle 2024-03-19 21:34:24 +01:00
parent ebcb1fee74
commit cb0ef2a035
3 changed files with 18 additions and 7 deletions

View File

@ -40,7 +40,7 @@ else()
)
# Add subdir with lwmem and link to the project
set(LWMEM_OPTS_DIR ${CMAKE_CURRENT_LIST_DIR}/dev)
set(LWMEM_OPTS_FILE ${CMAKE_CURRENT_LIST_DIR}/dev/lwmem_opts.h)
add_subdirectory(lwmem)
target_link_libraries(${PROJECT_NAME} lwmem)
target_link_libraries(${PROJECT_NAME} lwmem_cpp)

View File

@ -89,8 +89,8 @@ and it should be copied (or simply renamed in-place) and named ``lwmem_opts.h``
include paths have access to it by using ``#include "lwmem_opts.h"``.
.. tip::
If you are using *CMake* build system, define the variable ``LWMEM_OPTS_DIR`` before adding library's directory to the *CMake* project.
Variable must set the output directory path. CMake will copy the template file there, and name it as required.
If you are using *CMake* build system, define the variable ``LWMEM_OPTS_FILE`` before adding library's directory to the *CMake* project.
Variable must contain the path to the user options file. If not provided, one will be generated in the build directory.
Configuration options list is available available in the :ref:`api_lwmem_opt` section.
If any option is about to be modified, it should be done in configuration file

View File

@ -1,15 +1,20 @@
#
# LIB_PREFIX: LWMEM
#
# This file provides set of variables for end user
# and also generates one (or more) libraries, that can be added to the project using target_link_libraries(...)
#
# Before this file is included to the root CMakeLists file (using include() function), user can set some variables:
#
# LWMEM_SYS_PORT: If defined, it will include port source file from the library.
# LWMEM_OPTS_DIR: If defined, it should set the folder path where options file shall be generated.
# LWMEM_OPTS_FILE: If defined, it is the path to the user options file. If not defined, one will be generated for you automatically
# LWMEM_COMPILE_OPTIONS: If defined, it provide compiler options for generated library.
# LWMEM_COMPILE_DEFINITIONS: If defined, it provides "-D" definitions to the library build
#
# Custom include directory
set(LWMEM_CUSTOM_INC_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib_inc)
# Library core sources
set(lwmem_core_SRCS
${CMAKE_CURRENT_LIST_DIR}/src/lwmem/lwmem.c
@ -23,6 +28,7 @@ set(lwmem_core_cpp_SRCS
# Setup include directories
set(lwmem_include_DIRS
${CMAKE_CURRENT_LIST_DIR}/src/include
${LWMEM_CUSTOM_INC_DIR}
)
# Add system port
@ -46,7 +52,12 @@ target_include_directories(lwmem_cpp INTERFACE ${lwmem_include_DIRS})
target_compile_options(lwmem_cpp PRIVATE ${LWMEM_COMPILE_OPTIONS})
target_compile_definitions(lwmem_cpp PRIVATE ${LWMEM_COMPILE_DEFINITIONS})
# Create config file
if(DEFINED LWMEM_OPTS_DIR AND NOT EXISTS ${LWMEM_OPTS_DIR}/lwmem_opts.h)
configure_file(${CMAKE_CURRENT_LIST_DIR}/src/include/lwmem/lwmem_opts_template.h ${LWMEM_OPTS_DIR}/lwmem_opts.h COPYONLY)
# Create config file if user didn't provide one info himself
if(NOT LWMEM_OPTS_FILE)
message(STATUS "Using default lwmem_opts.h file")
set(LWMEM_OPTS_FILE ${CMAKE_CURRENT_LIST_DIR}/src/include/lwmem/lwmem_opts_template.h)
else()
message(STATUS "Using custom lwmem_opts.h file from ${LWMEM_OPTS_FILE}")
endif()
configure_file(${LWMEM_OPTS_FILE} ${LWMEM_CUSTOM_INC_DIR}/lwmem_opts.h COPYONLY)