RA-Consulting-GmbH / openscenario.api.test

Apache License 2.0
38 stars 11 forks source link

Optimize CMake build scripts #168

Open ahege opened 1 year ago

ahege commented 1 year ago

Is your feature request related to a problem? Please describe. Do some optimization on the build scripts.

Describe the solution you'd like To be described in the comments

ahege commented 1 year ago

Optimization:

Include directories

Skipped mist of the include_directories in the executable's CMake-files. Instead we use target_include_directories (with PUBLIC) to export the include directories in the libraries. There is no need of include_directoriesin the executable's Cmake file when the library is added via target_link_libraries. The include directories are automatically imported.

Outputput directory

We now let CMake select the platform dependent output directory via cmake-generator-expression We now have one output directory per artifact. Not a self define oputput directory (we used to name 'output') where everything is build into.

E.g. Output directory (Windows) for the artifact of 'OpenScenarioReader' with 'VS2017 x64 shared' and 'Debig' configuration is resolved by cmake-generator-expressions:

/openscenario.api.test/cpp/build/cgMultiVS2017x64Shared/applications/openScenarioReader/Debug

This requires post buld steps where we copy the depenedent artifacts into the target directory. E.g. We copy the dependent dlls into /openscenario.api.test/cpp/build/cgMultiVS2017x64Shared/applications/openScenarioTester/Debug to be able to run the Tests. We use cmake-generator-expressions for these post build steps, too.

In the executable CMake files for MSVC we set the debugging working directory with:

set_target_properties( ${PROJECT_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "$<TARGET_FILE_DIR:${PROJECT_NAME}>" )

And we aere using the cmake-generator-expression '$<TARGET_FILE_DIR:${PROJECT_NAME}>'

TODO: Transfer the new output directory into the documentation. #153

ahege commented 1 year ago

TODO: Review workflow scripts where the output directory is explicitly defined:

/workflows/buildArtifactsForPackage.yml

reneparis commented 1 year ago

Hey @ahege : We're working extensively on getting the OpenScenarioLib available (and all it's dependencies) via

find_package(OpenScenarioLib REQUIRED
...
target_link_libraries(OurProject OpenScenarioLib)

Is this finally possible?

ahege commented 1 year ago

@reneparis Yes, this should be possible. Please wait, we are working on the Linux build scripts for the dev branch.

First step (from one of our projects):

Include(FetchContent)
FetchContent_Declare(
    oscLib
    GIT_REPOSITORY  https://github.com/RA-Consulting-GmbH/openscenario.api.test.git
    GIT_TAG         dev
)

FetchContent_MakeAvailable(oscLib)

In a second step

target_link_libraries(OurProject OpenScenarioLib)

'OpenScenarioLib' is the name of the artifact in CMake. You may look up the way, we included the library in our OpenScenarioTester application. You probably have to set the options correctly like BUILD_SHARED_LIBS in your 'OurProject'.

As a third step, if you are using shared configuration. you may copy the shared library dependencies in your 'OurProject' as a post-build step:

To copy the antlr shared libraries:

if (BUILD_SHARED_LIBS)
       add_custom_command(TARGET OurProject
                   POST_BUILD
                   COMMAND ${CMAKE_COMMAND}
                           -E copy ${ANTLR4_RUNTIME_LIBRARIES}
                           "$<TARGET_FILE_DIR:OurProject>")
endif()

To copy the openSceanrioLib shared library

if (BUILD_SHARED_LIBS)
    add_custom_command(TARGET OurProject POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
                    "$<TARGET_FILE:OpenScenarioLib>"
                    "$<TARGET_FILE_DIR:OurProject>"
            COMMAND ${CMAKE_COMMAND} -E echo "Copying 'OpenScenarioLib' to '$<TARGET_FILE_DIR:OurProject>'"
           )
endif()

and finally he expressionLib shared library

if (BUILD_SHARED_LIBS)
    add_custom_command(TARGET OurProject POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
                    "$<TARGET_FILE:ExpressionLib>"
                    "$<TARGET_FILE_DIR:OurProject>"
            COMMAND ${CMAKE_COMMAND} -E echo "Copying 'ExpressionLib' to '$<TARGET_FILE_DIR:OurProject>'"
           )
endif()
reneparis commented 1 year ago

Hello @ahege Thanks for the answer. We'll reserve some time in our next sprint to test that.

ReinhardBiegelIntech commented 1 year ago

While using FetchContent is a feasible approach it doesn't cover the use case we have in the openPASS project. Due to build time considerations we have to fetch dependencies as binary packages (at least on the CI systems).

Of course anyone is free to pre-built their own binaries, but there would still be some cmake configuration missing (config/target files, see Config Mode). AFAIK this is the standard approach for cmake projects?

If packaging shall be included in the project itself, it might be accomplished by various mechanisms:

@ahege What are your thoughts about that?