Skip to content

Commit

Permalink
CMake: split object library by language, pass correct options to each.
Browse files Browse the repository at this point in the history
Previously target compile definitions weren't properly added to the
autogenerated sources, as they were built into a separate object library
(to ensure that they are built before the rest of the sources). Now the
object library is built separately for each language with proper options.

This addresses issue #515 "version 4.0.1 re2rust default generate C code".
  • Loading branch information
skvadrik committed Dec 10, 2024
1 parent 170ffa3 commit 9cfb87e
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ include_directories(. "${CMAKE_CURRENT_BINARY_DIR}")
# generated once at the beginning of build (as with Automake BUILT_SOURCES).
# WARNING: custom target that depends on autogenerated sources won't do here
# as its dependencies get built multiple times, which ruins parallel builds.
add_library(re2c_objects_autogen OBJECT

set(autogen_sources
"${CMAKE_CURRENT_BINARY_DIR}/src/options/parse_opts.cc"
"${CMAKE_CURRENT_BINARY_DIR}/src/parse/lexer.cc"
"${CMAKE_CURRENT_BINARY_DIR}/src/parse/lexer.h"
Expand All @@ -178,6 +179,42 @@ add_library(re2c_objects_autogen OBJECT
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_zig.cc"
"${re2c_docs}"
)

# We need a separate object lib for each language, as they have different compile definitions.

add_library(autogen_objects_c OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_c PUBLIC "RE2C_LANG=Lang::C" "RE2C_PROG=\"re2c\"")

add_library(autogen_objects_d OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_d PUBLIC "RE2C_LANG=Lang::D" "RE2C_PROG=\"re2d\"")

add_library(autogen_objects_go OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_go PUBLIC "RE2C_LANG=Lang::GO" "RE2C_PROG=\"re2go\"")

add_library(autogen_objects_hs OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_hs PUBLIC "RE2C_LANG=Lang::HASKELL" "RE2C_PROG=\"re2hs\"")

add_library(autogen_objects_java OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_java PUBLIC "RE2C_LANG=Lang::JAVA" "RE2C_PROG=\"re2java\"")

add_library(autogen_objects_js OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_js PUBLIC "RE2C_LANG=Lang::JS" "RE2C_PROG=\"re2js\"")

add_library(autogen_objects_ocaml OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_ocaml PUBLIC "RE2C_LANG=Lang::OCAML" "RE2C_PROG=\"re2ocaml\"")

add_library(autogen_objects_py OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_py PUBLIC "RE2C_LANG=Lang::PYTHON" "RE2C_PROG=\"re2py\"")

add_library(autogen_objects_rust OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_rust PUBLIC "RE2C_LANG=Lang::RUST" "RE2C_PROG=\"re2rust\"")

add_library(autogen_objects_v OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_v PUBLIC "RE2C_LANG=Lang::V" "RE2C_PROG=\"re2v\"")

add_library(autogen_objects_zig OBJECT "${autogen_sources}")
target_compile_definitions(autogen_objects_zig PUBLIC "RE2C_LANG=Lang::ZIG" "RE2C_PROG=\"re2zig\"")

add_library(re2c_objects_autogen_ver_to_vernum OBJECT
"${CMAKE_CURRENT_BINARY_DIR}/src/msg/ver_to_vernum.cc"
)
Expand Down Expand Up @@ -242,7 +279,6 @@ set(re2c_sources
src/util/string_utils.cc
src/util/range.cc
src/main.cc
$<TARGET_OBJECTS:re2c_objects_autogen>
$<TARGET_OBJECTS:re2c_objects_autogen_ver_to_vernum>
)

Expand Down Expand Up @@ -276,13 +312,13 @@ re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_c}")
add_custom_target(docs DEPENDS "${re2c_docs}")

# re2c
add_executable(re2c ${re2c_sources} "${re2c_help_c}")
add_executable(re2c ${re2c_sources} "${re2c_help_c}" $<TARGET_OBJECTS:autogen_objects_c>)

# re2d
if (RE2C_BUILD_RE2D)
re2c_gen_help("${re2c_help_source}" "${re2c_help_d}")
re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_d}")
add_executable(re2d ${re2c_sources} "${re2c_help_d}")
add_executable(re2d ${re2c_sources} "${re2c_help_d}" $<TARGET_OBJECTS:autogen_objects_d>)
target_compile_definitions(re2d PUBLIC
"RE2C_LANG=Lang::D"
"RE2C_PROG=\"re2d\""
Expand All @@ -293,7 +329,7 @@ endif()
if (RE2C_BUILD_RE2GO)
re2c_gen_help("${re2c_help_source}" "${re2c_help_go}")
re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_go}")
add_executable(re2go ${re2c_sources} "${re2c_help_go}")
add_executable(re2go ${re2c_sources} "${re2c_help_go}" $<TARGET_OBJECTS:autogen_objects_go>)
target_compile_definitions(re2go PUBLIC
"RE2C_LANG=Lang::GO"
"RE2C_PROG=\"re2go\""
Expand All @@ -304,7 +340,7 @@ endif()
if (RE2C_BUILD_RE2HS)
re2c_gen_help("${re2c_help_source}" "${re2c_help_haskell}")
re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_haskell}")
add_executable(re2hs ${re2c_sources} "${re2c_help_haskell}")
add_executable(re2hs ${re2c_sources} "${re2c_help_haskell}" $<TARGET_OBJECTS:autogen_objects_hs>)
target_compile_definitions(re2hs PUBLIC
"RE2C_LANG=Lang::HASKELL"
"RE2C_PROG=\"re2hs\""
Expand All @@ -315,7 +351,7 @@ endif()
if (RE2C_BUILD_RE2JAVA)
re2c_gen_help("${re2c_help_source}" "${re2c_help_java}")
re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_java}")
add_executable(re2java ${re2c_sources} "${re2c_help_java}")
add_executable(re2java ${re2c_sources} "${re2c_help_java}" $<TARGET_OBJECTS:autogen_objects_java>)
target_compile_definitions(re2java PUBLIC
"RE2C_LANG=Lang::JAVA"
"RE2C_PROG=\"re2java\""
Expand All @@ -326,7 +362,7 @@ endif()
if (RE2C_BUILD_RE2JS)
re2c_gen_help("${re2c_help_source}" "${re2c_help_js}")
re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_js}")
add_executable(re2js ${re2c_sources} "${re2c_help_js}")
add_executable(re2js ${re2c_sources} "${re2c_help_js}" $<TARGET_OBJECTS:autogen_objects_js>)
target_compile_definitions(re2js PUBLIC
"RE2C_LANG=Lang::JS"
"RE2C_PROG=\"re2js\""
Expand All @@ -337,7 +373,7 @@ endif()
if (RE2C_BUILD_RE2OCAML)
re2c_gen_help("${re2c_help_source}" "${re2c_help_ocaml}")
re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_ocaml}")
add_executable(re2ocaml ${re2c_sources} "${re2c_help_ocaml}")
add_executable(re2ocaml ${re2c_sources} "${re2c_help_ocaml}" $<TARGET_OBJECTS:autogen_objects_ocaml>)
target_compile_definitions(re2ocaml PUBLIC
"RE2C_LANG=Lang::OCAML"
"RE2C_PROG=\"re2ocaml\""
Expand All @@ -348,7 +384,7 @@ endif()
if (RE2C_BUILD_RE2PY)
re2c_gen_help("${re2c_help_source}" "${re2c_help_python}")
re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_python}")
add_executable(re2py ${re2c_sources} "${re2c_help_python}")
add_executable(re2py ${re2c_sources} "${re2c_help_python}" $<TARGET_OBJECTS:autogen_objects_py>)
target_compile_definitions(re2py PUBLIC
"RE2C_LANG=Lang::PYTHON"
"RE2C_PROG=\"re2py\""
Expand All @@ -359,7 +395,7 @@ endif()
if (RE2C_BUILD_RE2RUST)
re2c_gen_help("${re2c_help_source}" "${re2c_help_rust}")
re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_rust}")
add_executable(re2rust ${re2c_sources} "${re2c_help_rust}")
add_executable(re2rust ${re2c_sources} "${re2c_help_rust}" $<TARGET_OBJECTS:autogen_objects_rust>)
target_compile_definitions(re2rust PUBLIC
"RE2C_LANG=Lang::RUST"
"RE2C_PROG=\"re2rust\""
Expand All @@ -370,7 +406,7 @@ endif()
if (RE2C_BUILD_RE2V)
re2c_gen_help("${re2c_help_source}" "${re2c_help_v}")
re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_v}")
add_executable(re2v ${re2c_sources} "${re2c_help_v}")
add_executable(re2v ${re2c_sources} "${re2c_help_v}" $<TARGET_OBJECTS:autogen_objects_v>)
target_compile_definitions(re2v PUBLIC
"RE2C_LANG=Lang::V"
"RE2C_PROG=\"re2v\""
Expand All @@ -381,7 +417,7 @@ endif()
if (RE2C_BUILD_RE2ZIG)
re2c_gen_help("${re2c_help_source}" "${re2c_help_zig}")
re2c_gen_manpage("${re2c_manpage_source}" "${re2c_manpage_zig}")
add_executable(re2zig ${re2c_sources} "${re2c_help_zig}")
add_executable(re2zig ${re2c_sources} "${re2c_help_zig}" $<TARGET_OBJECTS:autogen_objects_zig>)
target_compile_definitions(re2zig PUBLIC
"RE2C_LANG=Lang::ZIG"
"RE2C_PROG=\"re2zig\""
Expand Down

0 comments on commit 9cfb87e

Please sign in to comment.