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:22 +01:00
parent 26fd03a88b
commit 5e6a15b22b
3 changed files with 18 additions and 8 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ else()
)
# Add subdir with lwbtn and link to project
set(LWBTN_OPTS_DIR ${CMAKE_CURRENT_LIST_DIR}/dev)
set(LWBTN_OPTS_FILE ${CMAKE_CURRENT_LIST_DIR}/dev/lwbtn_opts.h)
add_subdirectory(lwbtn)
target_link_libraries(${PROJECT_NAME} lwbtn)
endif()
+2 -2
View File
@@ -89,8 +89,8 @@ and it should be copied (or simply renamed in-place) and named ``lwbtn_opts.h``
include paths have access to it by using ``#include "lwbtn_opts.h"``.
.. tip::
If you are using *CMake* build system, define the variable ``LWBTN_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 ``LWBTN_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_lwbtn_opt` section.
If any option is about to be modified, it should be done in configuration file
+15 -5
View File
@@ -1,14 +1,19 @@
#
# LIB_PREFIX: LWBTN
#
# 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:
#
# LWBTN_OPTS_DIR: If defined, it should set the folder path where options file shall be generated.
# LWBTN_OPTS_FILE: If defined, it is the path to the user options file. If not defined, one will be generated for you automatically
# LWBTN_COMPILE_OPTIONS: If defined, it provide compiler options for generated library.
# LWBTN_COMPILE_DEFINITIONS: If defined, it provides "-D" definitions to the library build
#
# Custom include directory
set(LWBTN_CUSTOM_INC_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib_inc)
# Library core sources
set(lwbtn_core_SRCS
${CMAKE_CURRENT_LIST_DIR}/src/lwbtn/lwbtn.c
@@ -17,6 +22,7 @@ set(lwbtn_core_SRCS
# Setup include directories
set(lwbtn_include_DIRS
${CMAKE_CURRENT_LIST_DIR}/src/include
${LWBTN_CUSTOM_INC_DIR}
)
# Register library to the system
@@ -26,7 +32,11 @@ target_include_directories(lwbtn INTERFACE ${lwbtn_include_DIRS})
target_compile_options(lwbtn PRIVATE ${LWBTN_COMPILE_OPTIONS})
target_compile_definitions(lwbtn PRIVATE ${LWBTN_COMPILE_DEFINITIONS})
# Create config file
if(DEFINED LWBTN_OPTS_DIR AND NOT EXISTS ${LWBTN_OPTS_DIR}/lwbtn_opts.h)
configure_file(${CMAKE_CURRENT_LIST_DIR}/src/include/lwbtn/lwbtn_opts_template.h ${LWBTN_OPTS_DIR}/lwbtn_opts.h COPYONLY)
endif()
# Create config file if user didn't provide one info himself
if(NOT LWBTN_OPTS_FILE)
message(STATUS "Using default lwbtn_opts.h file")
set(LWBTN_OPTS_FILE ${CMAKE_CURRENT_LIST_DIR}/src/include/lwbtn/lwbtn_opts_template.h)
else()
message(STATUS "Using custom lwbtn_opts.h file from ${LWBTN_OPTS_FILE}")
endif()
configure_file(${LWBTN_OPTS_FILE} ${LWBTN_CUSTOM_INC_DIR}/lwbtn_opts.h COPYONLY)