Skip to content

Commit

Permalink
Update library CMake system, to support automatic creation of options…
Browse files Browse the repository at this point in the history
… file, if user doesn't provide one
  • Loading branch information
MaJerle committed Mar 19, 2024
1 parent f235fbd commit e063773
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 1 addition & 3 deletions CMakeLists.txt
Expand Up @@ -19,7 +19,6 @@ else()
${CMAKE_CURRENT_LIST_DIR}/examples/example_traverse.c
${CMAKE_CURRENT_LIST_DIR}/examples/example_stream.c
${CMAKE_CURRENT_LIST_DIR}/trial_env/trial_run.c

)

# Add key include paths
Expand All @@ -32,7 +31,6 @@ else()
WIN32
_DEBUG
CONSOLE
LWJSON_DEV
)

# Compiler options
Expand All @@ -43,7 +41,7 @@ else()
)

# Add subdir with lwjson and link to project
set(LWJSON_OPTS_DIR ${CMAKE_CURRENT_LIST_DIR}/dev)
set(LWJSON_OPTS_FILE ${CMAKE_CURRENT_LIST_DIR}/dev/lwjson_opts.h)
add_subdirectory(lwjson)
target_link_libraries(${PROJECT_NAME} lwjson)
target_link_libraries(${PROJECT_NAME} lwjson_debug)
Expand Down
4 changes: 2 additions & 2 deletions docs/get-started/index.rst
Expand Up @@ -89,8 +89,8 @@ and it should be copied (or simply renamed in-place) and named ``lwjson_opts.h``
include paths have access to it by using ``#include "lwjson_opts.h"``.

.. tip::
If you are using *CMake* build system, define the variable ``LWJSON_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 ``LWJSON_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_lwjson_opt` section.
If any option is about to be modified, it should be done in configuration file
Expand Down
20 changes: 15 additions & 5 deletions lwjson/library.cmake
@@ -1,14 +1,19 @@
#
# LIB_PREFIX: LWJSON
#
# 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:
#
# LWJSON_OPTS_DIR: If defined, it should set the folder path where options file shall be generated.
# LWJSON_OPTS_FILE: If defined, it is the path to the user options file. If not defined, one will be generated for you automatically
# LWJSON_COMPILE_OPTIONS: If defined, it provide compiler options for generated library.
# LWJSON_COMPILE_DEFINITIONS: If defined, it provides "-D" definitions to the library build
#

# Custom include directory
set(LWJSON_CUSTOM_INC_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib_inc)

# Setup generic source files
set(lwjson_core_SRCS
${CMAKE_CURRENT_LIST_DIR}/src/lwjson/lwjson.c
Expand All @@ -23,6 +28,7 @@ set(lwjson_debug_SRCS
# Setup include directories
set(lwjson_include_DIRS
${CMAKE_CURRENT_LIST_DIR}/src/include
${LWJSON_CUSTOM_INC_DIR}
)

# Register core library to the system
Expand All @@ -39,7 +45,11 @@ target_include_directories(lwjson_debug INTERFACE ${lwjson_include_DIRS})
target_compile_options(lwjson_debug PRIVATE ${LWJSON_COMPILE_OPTIONS})
target_compile_definitions(lwjson_debug PRIVATE ${LWJSON_COMPILE_DEFINITIONS})

# Create config file
if(DEFINED LWJSON_OPTS_DIR AND NOT EXISTS ${LWJSON_OPTS_DIR}/lwjson_opts.h)
configure_file(${CMAKE_CURRENT_LIST_DIR}/src/include/lwjson/lwjson_opts_template.h ${LWJSON_OPTS_DIR}/lwjson_opts.h COPYONLY)
endif()
# Create config file if user didn't provide one info himself
if(NOT LWJSON_OPTS_FILE)
message(STATUS "Using default lwjson_opts.h file")
set(LWJSON_OPTS_FILE ${CMAKE_CURRENT_LIST_DIR}/src/include/lwjson/lwjson_opts_template.h)
else()
message(STATUS "Using custom lwjson_opts.h file from ${LWJSON_OPTS_FILE}")
endif()
configure_file(${LWJSON_OPTS_FILE} ${LWJSON_CUSTOM_INC_DIR}/lwjson_opts.h COPYONLY)

0 comments on commit e063773

Please sign in to comment.