@@ -141,18 +141,46 @@ set(RULES_IR_DIR "${CMAKE_BINARY_DIR}/rules")
141141
142142set(RUSTLS_HEADER "${PROJECT_SOURCE_DIR}/rules/rustls/rustls.h")
143143
144+ set(PREPROCESSOR_BIN "${CMAKE_BINARY_DIR}/target_preprocessor/release/rule-preprocessor")
145+ set(PREPROCESSOR_STAMP "${CMAKE_BINARY_DIR}/.rule-preprocessor.stamp")
146+
147+ file(GLOB_RECURSE rule_preprocessor_sources
148+ ${PROJECT_SOURCE_DIR}/rule-preprocessor/src/*.rs)
149+
150+ execute_process(
151+ COMMAND ${CMAKE_COMMAND} -E env RUSTUP_TOOLCHAIN=${RUST_NIGHTLY_VERSION}
152+ rustc --print sysroot
153+ OUTPUT_VARIABLE NIGHTLY_SYSROOT
154+ OUTPUT_STRIP_TRAILING_WHITESPACE
155+ )
156+
157+ add_custom_command(
158+ OUTPUT ${PREPROCESSOR_BIN} ${RUSTLS_HEADER} ${PREPROCESSOR_STAMP}
159+ COMMAND cargo +${RUST_STABLE_VERSION} build --release
160+ --manifest-path "${PROJECT_SOURCE_DIR}/rules/Cargo.toml"
161+ COMMAND ${CMAKE_COMMAND} -E env
162+ CARGO_TARGET_DIR=${CMAKE_BINARY_DIR}/target_preprocessor
163+ cargo +${RUST_NIGHTLY_VERSION} build --release
164+ --manifest-path "${PROJECT_SOURCE_DIR}/rule-preprocessor/Cargo.toml"
165+ COMMAND ${CMAKE_COMMAND} -E touch "${PREPROCESSOR_STAMP}"
166+ DEPENDS ${rule_preprocessor_sources}
167+ "${PROJECT_SOURCE_DIR}/rules/build.rs"
168+ "${PROJECT_SOURCE_DIR}/rules/Cargo.toml"
169+ "${RUST_STAMP_FILE}"
170+ VERBATIM
171+ )
172+
144173file(GLOB rule_subdirs ${PROJECT_SOURCE_DIR}/rules/*)
145174set(cpp_rules_ir_outputs)
146175set(rust_rules_ir_outputs)
147- set(rust_rules_inputs)
148176foreach(_rule_dir IN LISTS rule_subdirs)
149- file(GLOB _srcs ${_rule_dir}/src.c ${_rule_dir}/src.cpp)
150- if(NOT _srcs )
177+ file(GLOB _cpp_srcs ${_rule_dir}/src.c ${_rule_dir}/src.cpp)
178+ if(NOT _cpp_srcs )
151179 continue()
152180 endif()
153181 get_filename_component(_rule_name ${_rule_dir} NAME)
154182 set(_out_dir ${RULES_IR_DIR}/${_rule_name})
155- set(_out ${_out_dir}/ir_src.json)
183+ set(_cpp_out ${_out_dir}/ir_src.json)
156184 set(_cxxflags)
157185 set(_cxxflags_file)
158186 if(EXISTS ${_rule_dir}/cxxflags)
@@ -163,39 +191,44 @@ foreach(_rule_dir IN LISTS rule_subdirs)
163191 endforeach()
164192 endif()
165193 add_custom_command(
166- OUTPUT ${_out }
194+ OUTPUT ${_cpp_out }
167195 COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir}
168- COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --dir ${_rule_dir} --out ${_out } ${_cxxflags}
169- DEPENDS ${_srcs } ${_cxxflags_file} ${PROJECT_SOURCE_DIR}/cpp2rust/cpp_rule_preprocessor.cpp ${RUSTLS_HEADER}
196+ COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --dir ${_rule_dir} --out ${_cpp_out } ${_cxxflags}
197+ DEPENDS ${_cpp_srcs } ${_cxxflags_file} ${PROJECT_SOURCE_DIR}/cpp2rust/cpp_rule_preprocessor.cpp ${RUSTLS_HEADER}
170198 VERBATIM
171199 )
172- list(APPEND cpp_rules_ir_outputs ${_out})
200+ list(APPEND cpp_rules_ir_outputs ${_cpp_out})
201+
202+ set(_rust_srcs)
203+ set(_rust_out)
173204 if(EXISTS ${_rule_dir}/tgt_unsafe.rs)
174- list(APPEND rust_rules_ir_outputs ${_out_dir}/ir_unsafe.json)
175- list(APPEND rust_rules_inputs ${_rule_dir}/tgt_unsafe.rs)
205+ list(APPEND _rust_out ${_out_dir}/ir_unsafe.json)
206+ list(APPEND _rust_srcs ${_rule_dir}/tgt_unsafe.rs)
176207 endif()
177208 if(EXISTS ${_rule_dir}/tgt_refcount.rs)
178- list(APPEND rust_rules_ir_outputs ${_out_dir}/ir_refcount.json)
179- list(APPEND rust_rules_inputs ${_rule_dir}/tgt_refcount.rs)
209+ list(APPEND _rust_out ${_out_dir}/ir_refcount.json)
210+ list(APPEND _rust_srcs ${_rule_dir}/tgt_refcount.rs)
180211 endif()
212+ if(NOT _rust_srcs)
213+ continue()
214+ endif()
215+ add_custom_command(
216+ OUTPUT ${_rust_out}
217+ COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir}
218+ COMMAND ${CMAKE_COMMAND} -E env
219+ CARGO_TARGET_DIR=${CMAKE_BINARY_DIR}/target_preprocessor
220+ RUSTUP_TOOLCHAIN=${RUST_NIGHTLY_VERSION}
221+ LD_LIBRARY_PATH=${NIGHTLY_SYSROOT}/lib
222+ DYLD_LIBRARY_PATH=${NIGHTLY_SYSROOT}/lib
223+ ${PREPROCESSOR_BIN} ${_out_dir} ${_rule_dir}
224+ DEPENDS ${_rust_srcs} ${PREPROCESSOR_BIN} ${PREPROCESSOR_STAMP}
225+ VERBATIM
226+ )
227+ list(APPEND rust_rules_ir_outputs ${_rust_out})
181228endforeach()
182229
183230add_custom_target("preprocess-cpp-rules" ALL DEPENDS ${cpp_rules_ir_outputs})
184231
185- file(GLOB_RECURSE rule_preprocessor_sources
186- ${PROJECT_SOURCE_DIR}/rule-preprocessor/src/*.rs)
187-
188- add_custom_command(
189- OUTPUT ${rust_rules_ir_outputs} ${RUSTLS_HEADER}
190- COMMAND cargo +${RUST_STABLE_VERSION} build --release --manifest-path "${PROJECT_SOURCE_DIR}/rules/Cargo.toml"
191- COMMAND ${CMAKE_COMMAND} -E env
192- CARGO_TARGET_DIR=${CMAKE_BINARY_DIR}/target_preprocessor
193- cargo +${RUST_NIGHTLY_VERSION} run --release --manifest-path "${PROJECT_SOURCE_DIR}/rule-preprocessor/Cargo.toml"
194- -- "${RULES_IR_DIR}"
195- DEPENDS ${rust_rules_inputs} ${rule_preprocessor_sources} "${RUST_STAMP_FILE}"
196- VERBATIM
197- )
198-
199232add_custom_target("preprocess-rust-rules" ALL
200233 DEPENDS ${rust_rules_ir_outputs})
201234
0 commit comments