CMakeLists: improve generated files
authorAlexander Couzens <lynxis@fe80.eu>
Sun, 1 Oct 2023 11:47:40 +0000 (13:47 +0200)
committerAlexander Couzens <lynxis@fe80.eu>
Sat, 9 Mar 2024 20:15:21 +0000 (21:15 +0100)
Flag custom command as PRE_BUILT.
Flag all generated files as generated.
Make generated .c files depend on generated .h files.
Move all generated files to the build dir.

Signed-off-by: Alexander Couzens <lynxis@fe80.eu>
CMakeLists.txt

index 4d1358e70c140e610a50be2eda9bf52577676ec3..7af35d78becc78dde8a59414e601e52e504df74e 100644 (file)
@@ -13,7 +13,6 @@ SET(SOURCES main.c dev.c commands.c qmi-message.c mbim.c)
 FIND_PATH(ubox_include_dir libubox/usock.h)
 FIND_PATH(blobmsg_json_include_dir libubox/blobmsg_json.h)
 FIND_PATH(json_include_dir json-c/json.h json/json.h json.h)
-INCLUDE_DIRECTORIES(${ubox_include_dir} ${blobmsg_json_include_dir} ${json_include_dir})
 
 IF(BUILD_STATIC)
   FIND_LIBRARY(json_library NAMES libjson.a libjson-c.a)
@@ -39,25 +38,31 @@ SET(service_headers)
 SET(service_sources)
 FOREACH(service ctl dms nas pds wds wms wda uim)
        ADD_CUSTOM_COMMAND(
-               OUTPUT  ${CMAKE_SOURCE_DIR}/qmi-message-${service}.h
-               COMMAND ${CMAKE_SOURCE_DIR}/data/gen-header.pl ${service}_ ${CMAKE_SOURCE_DIR}/data/qmi-service-${service}.json > ${CMAKE_SOURCE_DIR}/qmi-message-${service}.h
+               OUTPUT  qmi-message-${service}.h
+               PRE_BUILD
+               COMMAND ${CMAKE_SOURCE_DIR}/data/gen-header.pl ${service}_ ${CMAKE_SOURCE_DIR}/data/qmi-service-${service}.json > qmi-message-${service}.h
                DEPENDS ${CMAKE_SOURCE_DIR}/data/gen-header.pl ${CMAKE_SOURCE_DIR}/data/qmi-service-${service}.json ${CMAKE_SOURCE_DIR}/data/gen-common.pm
        )
+       SET_SOURCE_FILES_PROPERTIES(qmi-message-${service}.h PROPERTIES GENERATED 1)
        SET(service_headers ${service_headers} qmi-message-${service}.h)
        ADD_CUSTOM_COMMAND(
-               OUTPUT  ${CMAKE_SOURCE_DIR}/qmi-message-${service}.c
-               COMMAND ${CMAKE_SOURCE_DIR}/data/gen-code.pl ${service}_ ${CMAKE_SOURCE_DIR}/data/qmi-service-${service}.json > ${CMAKE_SOURCE_DIR}/qmi-message-${service}.c
-               DEPENDS ${CMAKE_SOURCE_DIR}/data/gen-code.pl ${CMAKE_SOURCE_DIR}/data/qmi-service-${service}.json ${CMAKE_SOURCE_DIR}/data/gen-common.pm
+               OUTPUT  qmi-message-${service}.c
+               PRE_BUILD
+               COMMAND ${CMAKE_SOURCE_DIR}/data/gen-code.pl ${service}_ ${CMAKE_SOURCE_DIR}/data/qmi-service-${service}.json > qmi-message-${service}.c
+               DEPENDS ${CMAKE_SOURCE_DIR}/data/gen-code.pl ${CMAKE_SOURCE_DIR}/data/qmi-service-${service}.json ${CMAKE_SOURCE_DIR}/data/gen-common.pm qmi-message-${service}.h
        )
        SET(service_sources ${service_sources} qmi-message-${service}.c)
+       SET_SOURCE_FILES_PROPERTIES(qmi-message-${service}.c PROPERTIES GENERATED 1)
        set_property(SOURCE qmi-message-${service}.c PROPERTY COMPILE_FLAGS "-Wno-unused")
 ENDFOREACH()
 
 ADD_CUSTOM_COMMAND(
        OUTPUT  ${CMAKE_SOURCE_DIR}/qmi-errors.c
-       COMMAND ${CMAKE_SOURCE_DIR}/data/gen-error-list.pl ${CMAKE_SOURCE_DIR}/qmi-errors.h > ${CMAKE_SOURCE_DIR}/qmi-errors.c
+       PRE_BUILD
+       COMMAND ${CMAKE_SOURCE_DIR}/data/gen-error-list.pl ${CMAKE_SOURCE_DIR}/qmi-errors.h > qmi-errors.c
        DEPENDS ${CMAKE_SOURCE_DIR}/data/gen-error-list.pl ${CMAKE_SOURCE_DIR}/qmi-errors.h
 )
+SET_SOURCE_FILES_PROPERTIES(qmi-errors.c PROPERTIES GENERATED 1)
 ADD_CUSTOM_TARGET(gen-errors DEPENDS qmi-errors.c)
 ADD_CUSTOM_TARGET(gen-headers DEPENDS ${service_headers})
 
@@ -65,6 +70,7 @@ ADD_EXECUTABLE(uqmi ${SOURCES} ${service_sources})
 ADD_DEPENDENCIES(uqmi gen-headers gen-errors)
 
 TARGET_LINK_LIBRARIES(uqmi ${LIBS})
+TARGET_INCLUDE_DIRECTORIES(uqmi PRIVATE ${ubox_include_dir} ${blobmsg_json_include_dir} ${json_include_dir} ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR})
 
 INSTALL(TARGETS uqmi
        RUNTIME DESTINATION sbin