Skip to content

Commit d271c84

Browse files
authored
Add lfc.cmake (#195)
1 parent 52dcc7c commit d271c84

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

cmake/lfc.cmake

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
# Sets up default values for LF_MAIN and LOG_LEVEL which
4+
# can be overridden by the user, either in the main CMakeLists.txt or
5+
# from the command line.
6+
function (lf_setup)
7+
if (NOT DEFINED LF_MAIN)
8+
message(FATAL_ERROR "LF_MAIN must be defined to be the name of the main LF source file without the .lf extension")
9+
endif()
10+
11+
if (NOT DEFINED LOG_LEVEL)
12+
set(LOG_LEVEL "LF_LOG_LEVEL_INFO" PARENT_SCOPE)
13+
endif()
14+
endfunction()
15+
16+
# Run the LFC compiler on the specified LF source file, LF_SOURCE_DIR/LF_MAIN.lf. Also make the CMake configuration
17+
# depend on any LF source file found within LF_SOURCE_DIR. This ensures that the LFC compiler is rerun whenever any LF
18+
# source file changes.
19+
# Args:
20+
# LF_SOURCE_DIR: The directory containing the LF source file.
21+
# LF_MAIN: The name of the LF source file without the .lf extension.
22+
function(lf_run_lfc LF_SOURCE_DIR LF_MAIN)
23+
# Check if the LF_SOURCE_DIR exists
24+
if (NOT EXISTS ${LF_SOURCE_DIR})
25+
message(FATAL_ERROR "LF source directory does not exist: ${LF_SOURCE_DIR}")
26+
endif()
27+
28+
# Check if the LF_MAIN file exists
29+
if (NOT EXISTS ${LF_SOURCE_DIR}/${LF_MAIN}.lf)
30+
message(FATAL_ERROR "LF main file does not exist: ${LF_SOURCE_DIR}/${LF_MAIN}.lf")
31+
endif()
32+
33+
set(LFC_COMMAND $ENV{REACTOR_UC_PATH}/lfc/bin/lfc-dev ${LF_SOURCE_DIR}/${LF_MAIN}.lf -o ${CMAKE_CURRENT_SOURCE_DIR})
34+
execute_process(COMMAND echo "Running LFC: ${LFC_COMMAND}")
35+
execute_process(
36+
COMMAND ${LFC_COMMAND}
37+
ECHO_OUTPUT_VARIABLE
38+
COMMAND_ERROR_IS_FATAL ANY
39+
40+
)
41+
file(GLOB_RECURSE LF_SOURCES ${LF_SOURCE_DIR}/*.lf)
42+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LF_SOURCES})
43+
message(STATUS "Found LF sources: ${LF_SOURCES}")
44+
45+
endfunction()
46+
47+
# Build the generated code from the LFC compiler. This function should be called after lf_run_lfc.
48+
# Args:
49+
# TARGET: The CMake target to build the generated code for. This target must already be defined.
50+
# SOURCE_GEN_DIR: The directory containing the generated code. This is typically src-gen/${LF_MAIN}.
51+
function(lf_build_generated_code TARGET SOURCE_GEN_DIR)
52+
# Check if the SOURCE_GEN_DIR exists
53+
if (NOT EXISTS ${SOURCE_GEN_DIR})
54+
message(FATAL_ERROR "src-gen directory does not exist: ${SOURCE_GEN_DIR}")
55+
endif()
56+
57+
# Check if the CMakeLists.txt file exists in SOURCE_GEN_DIR
58+
if (NOT EXISTS ${SOURCE_GEN_DIR}/CMakeLists.txt)
59+
message(FATAL_ERROR "CMakeLists.txt does not exist in src-gen directory: ${SOURCE_GEN_DIR}/CMakeLists.txt")
60+
endif()
61+
62+
include(${SOURCE_GEN_DIR}/CMakeLists.txt)
63+
add_subdirectory(${REACTOR_UC_PATH})
64+
target_sources(${TARGET} PRIVATE ${LFC_GEN_MAIN} ${LFC_GEN_SOURCES})
65+
target_include_directories(${TARGET} PRIVATE ${LFC_GEN_INCLUDE_DIRS})
66+
target_link_libraries(${TARGET} PUBLIC reactor-uc)
67+
target_compile_definitions(reactor-uc PUBLIC LF_LOG_LEVEL_ALL=${LOG_LEVEL})
68+
target_compile_definitions(reactor-uc PUBLIC ${LFC_GEN_COMPILE_DEFS})
69+
endfunction()

0 commit comments

Comments
 (0)