Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Zephyr's CMake extension to correctly build and link reactor-c #339

Merged
merged 10 commits into from
Jan 27, 2024
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ endif()

set(Test test)
set(Lib lib)
set(CoreLib core)
set(CoreLibPath core)
set(CoreLib reactor-c)
set(PlatformLib platform)

include_directories(${CMAKE_SOURCE_DIR}/include)
Expand All @@ -40,6 +41,6 @@ include_directories(${CMAKE_SOURCE_DIR}/include/api)
enable_testing()
add_subdirectory(${Test})
add_subdirectory(${Lib})
add_subdirectory(${CoreLib})
add_subdirectory(${CoreLibPath})

include(test/Tests.cmake)
65 changes: 37 additions & 28 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ if (DEFINED LF_TRACE)
list(APPEND GENERAL_SOURCES trace.c)
endif()

# Store all sources used to build the reactor-c lib in INFO_SOURCES
list(APPEND INFO_SOURCES ${GENERAL_SOURCES})

# Create the core library
add_library(core ${GENERAL_SOURCES})
# Add the general sources to the list of REACTORC_SOURCES
list(APPEND REACTORC_SOURCES ${GENERAL_SOURCES})

# Add sources for either threaded or single-threaded runtime
if (DEFINED FEDERATED)
Expand All @@ -25,8 +22,7 @@ endif()
if(DEFINED LF_SINGLE_THREADED)
message(STATUS "Including sources for single-threaded runtime.")
list(APPEND SINGLE_THREADED_SOURCES reactor.c)
target_sources(core PRIVATE ${SINGLE_THREADED_SOURCES})
list(APPEND INFO_SOURCES ${SINGLE_THREADED_SOURCES})
list(APPEND REACTORC_SOURCES ${SINGLE_THREADED_SOURCES})
else()
message(STATUS "Including sources for threaded runtime with \
${NUMBER_OF_WORKERS} worker(s) with scheduler=${SCHEDULER} and \
Expand All @@ -44,20 +40,33 @@ include(utils/CMakeLists.txt)
include(modal_models/CMakeLists.txt)
include(platform/CMakeLists.txt)


# Print sources used for compilation
list(JOIN INFO_SOURCES ", " PRINTABLE_SOURCE_LIST)
list(JOIN REACTORC_SOURCES ", " PRINTABLE_SOURCE_LIST)
message(STATUS "Including the following sources: " ${PRINTABLE_SOURCE_LIST})

target_include_directories(core PUBLIC ../include)
target_include_directories(core PUBLIC ../include/core)
target_include_directories(core PUBLIC ../include/core/federated)
target_include_directories(core PUBLIC ../include/core/federated/network)
target_include_directories(core PUBLIC ../include/core/platform)
target_include_directories(core PUBLIC ../include/core/modal_models)
target_include_directories(core PUBLIC ../include/core/threaded)
target_include_directories(core PUBLIC ../include/core/utils)
target_include_directories(core PUBLIC federated/RTI/)
# Create the reactor-c library. If we are targeting Zephyr we have to use the
# Zephyr Cmake extension to create the library and add the sources.
if(PLATFORM_ZEPHYR)
message("--- Building Zephyr library")
zephyr_library_named(reactor-c)
zephyr_library_sources(${REACTORC_SOURCES})
zephyr_library_link_libraries(kernel)
else()
add_library(reactor-c ${REACTORC_SOURCES})
endif()

# Apply compile definitions to the reactor-c library.
target_compile_definitions(reactor-c PUBLIC ${REACTORC_COMPILE_DEFS})

target_include_directories(reactor-c PUBLIC ../include)
target_include_directories(reactor-c PUBLIC ../include/core)
target_include_directories(reactor-c PUBLIC ../include/core/federated)
target_include_directories(reactor-c PUBLIC ../include/core/federated/network)
target_include_directories(reactor-c PUBLIC ../include/core/platform)
target_include_directories(reactor-c PUBLIC ../include/core/modal_models)
target_include_directories(reactor-c PUBLIC ../include/core/threaded)
target_include_directories(reactor-c PUBLIC ../include/core/utils)
target_include_directories(reactor-c PUBLIC federated/RTI/)

if (APPLE)
SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
Expand All @@ -72,38 +81,38 @@ if(DEFINED FEDERATED_AUTHENTICATED)
set(OPENSSL_ROOT_DIR /usr/local/opt/openssl)
endif()
find_package(OpenSSL REQUIRED)
target_link_libraries(core PUBLIC OpenSSL::SSL)
target_link_libraries(reactor-c PUBLIC OpenSSL::SSL)
endif()

if(DEFINED _LF_CLOCK_SYNC_ON)
find_library(MATH_LIBRARY m)
if(MATH_LIBRARY)
target_link_libraries(core PUBLIC ${MATH_LIBRARY})
target_link_libraries(reactor-c PUBLIC ${MATH_LIBRARY})
endif()
endif()

# Link with thread library, unless if we are targeting the Zephyr RTOS
# Link with thread library, unless we are on the Zephyr platform.
if(NOT DEFINED LF_SINGLE_THREADED OR DEFINED LF_TRACE)
if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Zephyr")
if (NOT PLATFORM_ZEPHYR)
find_package(Threads REQUIRED)
target_link_libraries(core PUBLIC Threads::Threads)
target_link_libraries(reactor-c PUBLIC Threads::Threads)
endif()
erlingrj marked this conversation as resolved.
Show resolved Hide resolved
endif()

# Macro for translating a command-line argument into compile definition for
# core lib
# reactor-c lib
macro(define X)
if(DEFINED ${X})
message(STATUS ${X}=${${X}})
target_compile_definitions(core PUBLIC ${X}=${${X}})
target_compile_definitions(reactor-c PUBLIC ${X}=${${X}})
endif(DEFINED ${X})
endmacro()

# FIXME: May want these to be application dependent, hence passed as
# parameters to Cmake.
target_compile_definitions(core PRIVATE INITIAL_EVENT_QUEUE_SIZE=10)
target_compile_definitions(core PRIVATE INITIAL_REACT_QUEUE_SIZE=10)
target_compile_definitions(core PUBLIC PLATFORM_${CMAKE_SYSTEM_NAME})
target_compile_definitions(reactor-c PRIVATE INITIAL_EVENT_QUEUE_SIZE=10)
target_compile_definitions(reactor-c PRIVATE INITIAL_REACT_QUEUE_SIZE=10)
target_compile_definitions(reactor-c PUBLIC PLATFORM_${CMAKE_SYSTEM_NAME})

# Search and apply all possible compile definitions
message(STATUS "Applying preprocessor definitions...")
Expand Down
64 changes: 46 additions & 18 deletions core/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ static void environment_init_modes(environment_t* env, int num_modes, int num_st
modes->modal_reactor_states_size = num_modes;
modes->triggered_reactions_request = 0;

modes->state_resets = (mode_state_variable_reset_data_t *) calloc(num_state_resets, sizeof(mode_state_variable_reset_data_t));
LF_ASSERT(modes->state_resets, "Out of memory");
modes->state_resets_size = num_state_resets;
if (modes->state_resets_size > 0) {
modes->state_resets = (mode_state_variable_reset_data_t *) calloc(modes->state_resets_size, sizeof(mode_state_variable_reset_data_t));
LF_ASSERT(modes->state_resets, "Out of memory");
} else {
modes->state_resets = NULL;
}

env->modes = modes;

Expand All @@ -107,9 +111,13 @@ static void environment_init_modes(environment_t* env, int num_modes, int num_st
*/
static void environment_init_federated(environment_t* env, int num_is_present_fields) {
#ifdef FEDERATED_DECENTRALIZED
env->_lf_intended_tag_fields = (tag_t**) calloc(num_is_present_fields, sizeof(tag_t*));
LF_ASSERT(env->_lf_intended_tag_fields, "Out of memory");
env->_lf_intended_tag_fields_size = num_is_present_fields;
if (num_is_present_fields > 0) {
env->_lf_intended_tag_fields = (tag_t**) calloc(num_is_present_fields, sizeof(tag_t*));
LF_ASSERT(env->_lf_intended_tag_fields, "Out of memory");
env->_lf_intended_tag_fields_size = num_is_present_fields;
} else {
env->_lf_intended_tag_fields_size = NULL;
}
#endif
}

Expand Down Expand Up @@ -197,29 +205,49 @@ int environment_init(
env->stop_tag = FOREVER_TAG;

env->timer_triggers_size=num_timers;
env->timer_triggers = (trigger_t **) calloc(num_timers, sizeof(trigger_t));
LF_ASSERT(env->timer_triggers, "Out of memory");
if(env->timer_triggers_size > 0) {
env->timer_triggers = (trigger_t **) calloc(num_timers, sizeof(trigger_t));
LF_ASSERT(env->timer_triggers, "Out of memory");
} else {
env->timer_triggers = NULL;
}

env->startup_reactions_size=num_startup_reactions;
env->startup_reactions = (reaction_t **) calloc(num_startup_reactions, sizeof(reaction_t));
LF_ASSERT(env->startup_reactions, "Out of memory");
if (env->startup_reactions_size > 0) {
env->startup_reactions = (reaction_t **) calloc(num_startup_reactions, sizeof(reaction_t));
LF_ASSERT(env->startup_reactions, "Out of memory");
} else {
env->startup_reactions = NULL;
}

env->shutdown_reactions_size=num_shutdown_reactions;
env->shutdown_reactions = (reaction_t **) calloc(num_shutdown_reactions, sizeof(reaction_t));
LF_ASSERT(env->shutdown_reactions, "Out of memory");
if(env->shutdown_reactions_size > 0) {
env->shutdown_reactions = (reaction_t **) calloc(num_shutdown_reactions, sizeof(reaction_t));
LF_ASSERT(env->shutdown_reactions, "Out of memory");
} else {
env->shutdown_reactions = NULL;
}

env->reset_reactions_size=num_reset_reactions;
env->reset_reactions = (reaction_t **) calloc(num_reset_reactions, sizeof(reaction_t));
LF_ASSERT(env->reset_reactions, "Out of memory");
if (env->reset_reactions_size > 0) {
env->reset_reactions = (reaction_t **) calloc(num_reset_reactions, sizeof(reaction_t));
LF_ASSERT(env->reset_reactions, "Out of memory");
} else {
env->reset_reactions = NULL;
}

env->is_present_fields_size = num_is_present_fields;
env->is_present_fields_abbreviated_size = 0;

env->is_present_fields = (bool**)calloc(num_is_present_fields, sizeof(bool*));
LF_ASSERT(env->is_present_fields, "Out of memory");

env->is_present_fields_abbreviated = (bool**)calloc(num_is_present_fields, sizeof(bool*));
LF_ASSERT(env->is_present_fields_abbreviated, "Out of memory");
if (env->is_present_fields_size > 0) {
env->is_present_fields = (bool**)calloc(num_is_present_fields, sizeof(bool*));
LF_ASSERT(env->is_present_fields, "Out of memory");
env->is_present_fields_abbreviated = (bool**)calloc(num_is_present_fields, sizeof(bool*));
LF_ASSERT(env->is_present_fields_abbreviated, "Out of memory");
} else {
env->is_present_fields = NULL;
env->is_present_fields_abbreviated = NULL;
}

env->_lf_handle=1;

Expand Down
3 changes: 1 addition & 2 deletions core/federated/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
set(FEDERATED_SOURCES clock-sync.c federate.c)
list(APPEND INFO_SOURCES ${FEDERATED_SOURCES})

list(TRANSFORM FEDERATED_SOURCES PREPEND federated/)
target_sources(core PRIVATE ${FEDERATED_SOURCES})
list(APPEND REACTORC_SOURCES ${FEDERATED_SOURCES})
3 changes: 1 addition & 2 deletions core/federated/network/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
set(LF_NETWORK_FILES net_util.c)
list(APPEND INFO_SOURCES ${LF_NETWORK_FILES})

list(TRANSFORM LF_NETWORK_FILES PREPEND federated/network/)
target_sources(core PRIVATE ${LF_NETWORK_FILES})
list(APPEND REACTORC_SOURCES ${LF_NETWORK_FILES})
3 changes: 1 addition & 2 deletions core/modal_models/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
set(MODAL_SOURCES modes.c)
list(APPEND INFO_SOURCES ${MODAL_SOURCES})

list(TRANSFORM MODAL_SOURCES PREPEND modal_models/)
target_sources(core PRIVATE ${MODAL_SOURCES})
list(APPEND REACTORC_SOURCES ${MODAL_SOURCES})
34 changes: 16 additions & 18 deletions core/platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@
# file and assign the file's path to LF_PLATFORM_FILE

set(LF_PLATFORM_FILES
lf_unix_clock_support.c
lf_unix_syscall_support.c
lf_linux_support.c
lf_macos_support.c
lf_windows_support.c
lf_nrf52_support.c
lf_zephyr_support.c
lf_zephyr_clock_counter.c
lf_zephyr_clock_kernel.c
lf_rp2040_support.c
lf_unix_clock_support.c
lf_unix_syscall_support.c
lf_linux_support.c
lf_macos_support.c
lf_windows_support.c
lf_nrf52_support.c
lf_zephyr_support.c
lf_zephyr_clock_counter.c
lf_zephyr_clock_kernel.c
lf_rp2040_support.c
)

if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
set(CMAKE_SYSTEM_VERSION 10.0)
message("Using Windows SDK version ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Nrf52")
target_compile_definitions(core PUBLIC PLATFORM_NRF52)
list(APPEND REACTORC_COMPILE_DEFS PLATFORM_NRF52)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Zephyr")
target_compile_definitions(core PUBLIC PLATFORM_ZEPHYR)
list(APPEND REACTORC_COMPILE_DEFS PLATFORM_ZEPHYR)
set(PLATFORM_ZEPHYR true)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Rp2040")
target_compile_definitions(core PUBLIC PLATFORM_RP2040)
list(APPEND REACTORC_COMPILE_DEFS PLATFORM_RP2040)
endif()

# Add sources to the list for debug info
list(APPEND INFO_SOURCES ${LF_PLATFORM_FILES})

# Prepend all sources with platform
list(TRANSFORM LF_PLATFORM_FILES PREPEND platform/)

# Add sources to core lib
target_sources(core PRIVATE ${LF_PLATFORM_FILES})
# Add sources to the list for debug info
list(APPEND REACTORC_SOURCES ${LF_PLATFORM_FILES})
3 changes: 1 addition & 2 deletions core/threaded/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ set(
scheduler_instance.c
watchdog.c
)
list(APPEND INFO_SOURCES ${THREADED_SOURCES})

list(TRANSFORM THREADED_SOURCES PREPEND threaded/)
target_sources(core PRIVATE ${THREADED_SOURCES})
list(APPEND REACTORC_SOURCES ${THREADED_SOURCES})

3 changes: 1 addition & 2 deletions core/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
set(UTIL_SOURCES vector.c pqueue_base.c pqueue_tag.c pqueue.c util.c semaphore.c)

list(APPEND INFO_SOURCES ${UTIL_SOURCES})

list(TRANSFORM UTIL_SOURCES PREPEND utils/)
target_sources(core PRIVATE ${UTIL_SOURCES})
list(APPEND REACTORC_SOURCES ${UTIL_SOURCES})

# Include sources from subdirectories
include(utils/hashset/CMakeLists.txt)
3 changes: 1 addition & 2 deletions core/utils/hashset/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
set(HASHSET_SOURCES hashset.c hashset_itr.c)

list(APPEND INFO_SOURCES ${HASHSET_SOURCES})

list(TRANSFORM HASHSET_SOURCES PREPEND utils/hashset/)
target_sources(core PRIVATE ${HASHSET_SOURCES})
list(APPEND REACTORC_SOURCES ${HASHSET_SOURCES})
Loading