From c3cc2985a9b0e27c56c26d1083462a6e9eabaebe Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Sun, 5 Mar 2023 15:57:03 -0500 Subject: [PATCH] WIP working on followup to https://github.com/chipsalliance/Surelog/pull/3182 to allow for removal of vendored libraries and relying instead on system-installed deps for better packaging Tweaking include directories, leads to CMake error fix flatbuffers install on ubuntu fix antlr4 apt package name, shouldve just used my ubuntu machine from the start.. prune build matrix to exclude no-vendoring on most strategies fix typo in action, add uhdm include path back fix cmake additional flags export syntax in github action clean up includes a bit when vendoring UHDM tweaks find uhdm, fixes for include paths and library links tweak find paths for antlr jar see where antlr assets end up tweak jar path as well install antlr manually Fix typo in capnproto library name fix missing install dir inclusion in cmake file update test install to work with external deps more tweaks for tests --- .github/workflows/main.yml | 66 +++++++++++- CMakeLists.txt | 147 +++++++++++++++++--------- Makefile | 24 ++++- cmake/modules/FindANTLR.cmake | 174 +++++++++++++++++++++++++++++++ cmake/modules/FindUHDM.cmake | 48 +++++++++ tests/TestInstall/CMakeLists.txt | 59 ++++++++--- 6 files changed, 444 insertions(+), 74 deletions(-) create mode 100644 cmake/modules/FindANTLR.cmake create mode 100644 cmake/modules/FindUHDM.cmake diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 59a48653f5..1240fa30c8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,7 +12,7 @@ jobs: # Linux build, test, and publish linux-install: - name: "Linux | Install | ${{ matrix.compiler }} | ${{ matrix.config }}" + name: "Linux | Install | ${{ matrix.compiler }} | ${{ matrix.config }} | ${{ matrix.vendored_dependencies }}" runs-on: ubuntu-20.04 strategy: fail-fast: false @@ -23,6 +23,14 @@ jobs: config: - debug - release + vendored_dependencies: [true, false] + exclude: + # Unnecessary to test no-vendoring across every strategy + - compiler: clang + vendored_dependencies: false + - config: debug + vendored_dependencies: false + env: artifact-name: sl-${{ github.run_number }}-linux-${{ matrix.compiler }}-${{ matrix.config }} @@ -32,12 +40,47 @@ jobs: with: submodules: recursive fetch-depth: 0 + if: ${{ matrix.vendored_dependencies }} + + - name: Checkout code + uses: actions/checkout@v3 + if: ${{ !matrix.vendored_dependencies }} - uses: ./.github/linux-setup with: compiler: ${{ matrix.compiler }} ccache-key: linux-install-${{ matrix.compiler }}-${{ matrix.config }} + - name: Install vendored dependencies + run: | + git clone https://github.com/google/flatbuffers.git + pushd flatbuffers + cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=17 . && cmake --build build && sudo cmake --install build + popd + + git clone https://github.com/google/googletest.git + pushd googletest + cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=17 . && cmake --build build && sudo cmake --install build + popd + + git clone https://github.com/capnproto/capnproto.git + pushd capnproto + cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=17 . && cmake --build build && sudo cmake --install build + popd + + git clone https://github.com/chipsalliance/UHDM.git + pushd UHDM + cmake -B build -DCMAKE_BUILD_TYPE=Release -DUHDM_USE_HOST_GTEST=ON -DUHDM_USE_HOST_CAPNP=ON . && cmake --build build && sudo cmake --install build + popd + + sudo mkdir -p /usr/share/java + sudo wget https://www.antlr.org/download/antlr-4.12.0-complete.jar -P /usr/share/java + wget https://www.antlr.org/download/antlr4-cpp-runtime-4.12.0-source.zip && mkdir antlr4 + pushd antlr4 + unzip ../antlr4-cpp-runtime-4.12.0-source.zip && cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=17 . && cmake --build build && sudo cmake --install build + popd + if: ${{ !matrix.vendored_dependencies }} + - name: Build, install & test run: | if [ "${{ matrix.config }}" == "debug" ]; then @@ -47,10 +90,15 @@ jobs: export BUILD_TYPE=Release export CMAKE_ADDITIONAL_ARGS= fi - + export INSTALL_DIR=`pwd`/install - cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR $CMAKE_ADDITIONAL_ARGS -S . -B build + if [ "${{ matrix.vendored_dependencies }}" == "false" ]; then + cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR -DSURELOG_USE_HOST_FLATBUFFERS=ON -DSURELOG_USE_HOST_ANTLR=ON -DSURELOG_USE_HOST_UHDM=ON -DSURELOG_USE_HOST_GTEST=ON -S . -B build + else + cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR $CMAKE_ADDITIONAL_ARGS -S . -B build + fi + cmake --build build -j $(nproc) cmake --install build @@ -58,7 +106,13 @@ jobs: pushd build && ctest --output-on-failure && popd rm -rf build # make sure we only see installation artifacts - cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DINSTALL_DIR=$INSTALL_DIR -S tests/TestInstall -B tests/TestInstall/build + + if [ "${{ matrix.vendored_dependencies }}" == "false" ]; then + cmake -DCMAKE_BUILD_TYPE=Release -DINSTALL_DIR=$INSTALL_DIR -DCMAKE_INSTALL_PREFIX=/home/timkpaine/Developer/programs/dau/third/Surelog/install -DSURELOG_USE_HOST_FLATBUFFERS=ON -DSURELOG_USE_HOST_ANTLR=ON -DSURELOG_USE_HOST_UHDM=ON -DSURELOG_USE_HOST_GTEST=ON -S tests/TestInstall -B tests/TestInstall/build + else + cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DINSTALL_DIR=$INSTALL_DIR -S tests/TestInstall -B tests/TestInstall/build + fi + cmake --build tests/TestInstall/build -j $(nproc) - name: Prepare build artifacts @@ -66,12 +120,14 @@ jobs: mkdir artifacts mv install ${{ env.artifact-name }} tar czfp artifacts/${{ env.artifact-name }}.tar.gz ${{ env.artifact-name }} + if: ${{ matrix.vendored_dependencies }} - name: Upload build artifacts uses: actions/upload-artifact@v3 with: name: ${{ env.artifact-name }} path: artifacts/${{ env.artifact-name }}.tar.gz + if: ${{ matrix.vendored_dependencies }} # Linux regression @@ -957,7 +1013,7 @@ jobs: python3 scripts/regression.py run \ --uhdm-dump-filepath bin/uhdm-dump \ - --jobs $(nproc) \ + --jobs $(sysctl -n hw.physicalcpu) \ --show-diffs \ --num_shards=${{ matrix.num_shards }} \ --shard=${{ matrix.shard }} diff --git a/CMakeLists.txt b/CMakeLists.txt index 66ff77be26..ccd05793e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ # -*- mode:cmake -*- -cmake_minimum_required(VERSION 3.15 FATAL_ERROR) +cmake_minimum_required(VERSION 3.20 FATAL_ERROR) # Detect build type, fallback to release and throw a warning if use didn't # specify any @@ -13,6 +13,8 @@ if(NOT CMAKE_BUILD_TYPE) FORCE) endif(NOT CMAKE_BUILD_TYPE) +set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules" ${CMAKE_MODULE_PATH}) + option( WITH_LIBCXX "If buildling with clang++ and libc++(in Linux). To enable with: -DWITH_LIBCXX=On" @@ -21,14 +23,17 @@ option( option( SURELOG_USE_HOST_FLATBUFFERS "Use flatbuffers library from host instead of third_party" OFF) - -option(SURELOG_BUILD_TESTS "Enable testing." ON) - -# TODO SURELOG_USE_HOST_CAPNP -# TODO SURELOG_USE_HOST_ANTLR -# TODO SURELOG_USE_HOST_UHDM +option( + SURELOG_USE_HOST_ANTLR + "Use Antlr library from host instead of third_party" OFF) +option( + SURELOG_USE_HOST_UHDM + "Use UHDM library from host instead of third_party" OFF) +option( + SURELOG_USE_HOST_GTEST + "Use googletest library from host instead of third_party" OFF) -include(FindPkgConfig) +option(SURELOG_BUILD_TESTS "Enable testing." ON) project(SURELOG) @@ -47,19 +52,44 @@ endif() set(FLATBUFFERS_FLATC_EXECUTABLE "flatc") if(SURELOG_USE_HOST_FLATBUFFERS) - pkg_check_modules(FLATBUFFERS IMPORTED_TARGET REQUIRED flatbuffers>=2.0.7) + find_package(Flatbuffers REQUIRED) + set(FLATBUFFERS_LIBRARY flatbuffers::flatbuffers) else() set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "Skip flatbuffers' tests") add_subdirectory(third_party/flatbuffers EXCLUDE_FROM_ALL) + set(FLATBUFFERS_LIBRARY flatbuffers) endif() set(WITH_STATIC_CRT ON CACHE BOOL "Use Static CRT") set(ANTLR_BUILD_CPP_TESTS OFF CACHE BOOL "Skip ANTLR tests") -add_subdirectory(third_party/antlr4/runtime/Cpp EXCLUDE_FROM_ALL) + +if(SURELOG_USE_HOST_ANTLR) + find_package(ANTLR REQUIRED) +else() + add_subdirectory(third_party/antlr4/runtime/Cpp EXCLUDE_FROM_ALL) + set(ANTLR_JAR_LOCATION ${PROJECT_SOURCE_DIR}/third_party/antlr4_bin/antlr-4.12.0-complete.jar) + set(ANTLR_LIBRARY antlr4_static) + set(ANTLR_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/third_party/antlr4/runtime/Cpp/runtime/src) +endif() set(UHDM_BUILD_TESTS OFF CACHE BOOL "Skip UHDM tests") -add_subdirectory(third_party/UHDM) -add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL) +if(SURELOG_USE_HOST_UHDM) + find_package(UHDM REQUIRED) + find_package(CapnProto) + set(CAPNPROTO_LIBRARY CapnProto::capnp) +else() + add_subdirectory(third_party/UHDM) + set(UHDM_LIBRARY uhdm) + get_target_property(UHDM_INCLUDE_DIRS uhdm INCLUDE_DIRECTORIES) + set(CAPNPROTO_LIBRARY capnp) +endif() + +if(SURELOG_USE_HOST_GTEST) + find_package(GTest REQUIRED) +else() + add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL) + set(GTEST_INCLUDE_DIRS third_party/googletest/googletest/include third_party/googletest/googlemock/include) +endif() # NOTE: Set the global output directories after the subprojects have had their go at it set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) @@ -162,8 +192,6 @@ add_custom_command( # Java find_package(Java 11 REQUIRED COMPONENTS Runtime) message(STATUS "Java_JAVA_EXECUTABLE = ${Java_JAVA_EXECUTABLE}") -set(ANTLR_JAR_LOCATION - ${PROJECT_SOURCE_DIR}/third_party/antlr4_bin/antlr-4.12.0-complete.jar) add_custom_target(GenerateParser DEPENDS ${GENDIR}/src/parser/generated-parsers.tstamp @@ -426,14 +454,25 @@ foreach(gen_src ${surelog_generated_SRC}) set_source_files_properties(${gen_src} PROPERTIES GENERATED TRUE) endforeach() -add_library(surelog STATIC ${surelog_SRC} ${surelog_generated_SRC}) +add_library(surelog ${surelog_SRC} ${surelog_generated_SRC}) set_target_properties(surelog PROPERTIES PUBLIC_HEADER include/Surelog/surelog.h) + +if(BUILD_SHARED_LIBS) + set_property(TARGET surelog PROPERTY POSITION_INDEPENDENT_CODE 1) +endif() + target_include_directories(surelog PUBLIC $ $) target_include_directories(surelog PUBLIC $ $) +target_include_directories(surelog PUBLIC + $ + $) +target_include_directories(surelog PUBLIC + $ + $) if (SURELOG_WITH_PYTHON) target_include_directories(surelog PUBLIC ${Python3_INCLUDE_DIRS}) # Keep this at the end @@ -478,20 +517,28 @@ if(MSVC OR WIN32) endif() target_link_libraries(surelog-bin PUBLIC surelog) -target_link_libraries(surelog PUBLIC antlr4_static) +target_link_libraries(surelog PUBLIC ${ANTLR_LIBRARY}) +target_include_directories(surelog PUBLIC ${FLATBUFFERS_INCLUDE_DIRS}) -if(SURELOG_USE_HOST_FLATBUFFERS) - target_link_libraries(surelog PRIVATE PkgConfig::FLATBUFFERS) - target_include_directories(surelog PUBLIC ${FLATBUFFERS_INCLUDE_DIRS}) -else() - target_link_libraries(surelog PUBLIC flatbuffers) +target_link_libraries(surelog PRIVATE ${FLATBUFFERS_LIBRARY}) +if(NOT SURELOG_USE_HOST_FLATBUFFERS) add_dependencies(surelog flatc) endif() -target_link_libraries(surelog PUBLIC uhdm) +target_link_libraries(surelog PUBLIC ${UHDM_LIBRARY}) +target_link_libraries(surelog PRIVATE ${CAPNPROTO_LIBRARY}) + +if(NOT SURELOG_USE_HOST_UHDM) + add_dependencies(GenerateSerializer ${UHDM_LIBRARY}) + target_include_directories(surelog PUBLIC + $ + $) +endif() + +if(NOT SURELOG_USE_HOST_ANTLR) + add_dependencies(GenerateParser ${ANTLR_LIBRARY}) +endif() -add_dependencies(GenerateSerializer uhdm) -add_dependencies(GenerateParser antlr4_static) add_dependencies(surelog GenerateParser) add_dependencies(surelog GenerateSerializer) @@ -515,7 +562,9 @@ if (UNIX) endif() if (CMAKE_SYSTEM_NAME MATCHES "Linux") - target_link_libraries(uhdm PRIVATE stdc++fs) + if(NOT SURELOG_USE_HOST_UHDM) + target_link_libraries(uhdm PRIVATE stdc++fs) + endif() target_link_libraries(surelog PRIVATE stdc++fs) target_link_libraries(surelog PRIVATE rt) endif() @@ -556,7 +605,7 @@ if (SURELOG_BUILD_TESTS) # For simplicity, we link the test with libsurelog, but there is of # course a lot unnecessary churn if headers are modified. # Often it is sufficient to just have a few depeendencies. - target_link_libraries(${test_bin} surelog gtest gmock gtest_main) + target_link_libraries(${test_bin} surelog GTest::gtest GTest::gmock GTest::gtest_main) gtest_discover_tests(${test_bin} TEST_PREFIX "${test_prefix}/" DISCOVERY_TIMEOUT 60) @@ -658,6 +707,8 @@ add_dependencies(hellosureworld PrecompileOVM) add_dependencies(hellosureworld PrecompileUVM) endif() +include(GNUInstallDirs) + # Installation target if (QUICK_COMP) install( @@ -676,14 +727,13 @@ install( ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/surelog PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/Surelog) -# TODO: SURELOG_USE_HOST_ANTLR -install( - TARGETS antlr4_static - EXPORT Surelog - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/surelog - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/Surelog) -target_include_directories(surelog PRIVATE - third_party/antlr4/runtime/Cpp/runtime/src) +if(NOT SURELOG_USE_HOST_ANTLR) + install( + TARGETS antlr4_static + EXPORT Surelog + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/surelog + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/Surelog) +endif() if(NOT SURELOG_USE_HOST_FLATBUFFERS) # TODO: should we even install these from third_party ? If it is @@ -696,19 +746,19 @@ if(NOT SURELOG_USE_HOST_FLATBUFFERS) PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/Surelog) endif() -# TODO SURELOG_USE_HOST_CAPNP -install( - TARGETS capnp kj - EXPORT Surelog - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/uhdm - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/uhdm) +if(NOT SURELOG_USE_HOST_UHDM) + install( + TARGETS capnp kj + EXPORT Surelog + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/uhdm + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/uhdm) -# TODO: SURELOG_USE_HOST_UHDM -install( - TARGETS uhdm - EXPORT Surelog - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/uhdm - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/uhdm) + install( + TARGETS uhdm + EXPORT Surelog + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/uhdm + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/uhdm) +endif() if (SURELOG_WITH_PYTHON) install( @@ -716,8 +766,7 @@ if (SURELOG_WITH_PYTHON) DESTINATION ${CMAKE_INSTALL_LIBDIR}/surelog) endif() -install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/pkg - DESTINATION ${CMAKE_INSTALL_BINDIR}) +install(DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/pkg DESTINATION ${CMAKE_INSTALL_BINDIR}) install( FILES ${PROJECT_SOURCE_DIR}/include/Surelog/CommandLine/CommandLineParser.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/Surelog/CommandLine) @@ -850,6 +899,8 @@ install( EXPORT Surelog FILE Surelog.cmake DESTINATION cmake) + +# Generate cmake config files for reuse by downstream packages include(CMakePackageConfigHelpers) # generate the config file that is includes the exports diff --git a/Makefile b/Makefile index 6e16b8edd0..3976ebcf47 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,16 @@ ifdef $(LC_ALL) undefine LC_ALL endif +UNAME := $(shell uname) +ifeq ($(UNAME), Linux) +NPROC = $(shell nproc) +endif +ifeq ($(UNAME), Darwin) +NPROC = $(shell sysctl -n hw.physicalcpu) +endif + ifeq ($(CPU_CORES),) - CPU_CORES := $(shell nproc) - ifeq ($(CPU_CORES),) - CPU_CORES := $(shell sysctl -n hw.physicalcpu) - endif + CPU_CORES := $(NPROC) ifeq ($(CPU_CORES),) CPU_CORES := 2 # Good minimum assumption endif @@ -22,6 +27,9 @@ export CTEST_PARALLEL_LEVEL = $(CPU_CORES) release: run-cmake-release cmake --build build -j $(CPU_CORES) +release-shared: run-cmake-release-shared + cmake --build build -j $(CPU_CORES) + release_with_python: run-cmake-release-with-python cmake --build build -j $(CPU_CORES) @@ -35,7 +43,10 @@ quick: run-cmake-quick cmake --build dbuild -j $(CPU_CORES) run-cmake-release: - cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$(PREFIX) $(ADDITIONAL_CMAKE_OPTIONS) -S . -B build + cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$(PREFIX) -DBUILD_SHARED_LIBS=OFF $(ADDITIONAL_CMAKE_OPTIONS) -S . -B build + +run-cmake-release-shared: + cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$(PREFIX) -DBUILD_SHARED_LIBS=ON $(ADDITIONAL_CMAKE_OPTIONS) -S . -B build run-cmake-release-with-python: cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$(PREFIX) $(ADDITIONAL_CMAKE_OPTIONS) -DSURELOG_WITH_PYTHON=1 -DCMAKE_CXX_FLAGS=-fpermissive -S . -B build @@ -119,6 +130,9 @@ clean: install: release cmake --install build +install-shared: release-shared + cmake --install build + test_install: cmake -DCMAKE_BUILD_TYPE=Release -DINSTALL_DIR=$(PREFIX) -S tests/TestInstall -B tests/TestInstall/build cmake --build tests/TestInstall/build -j $(CPU_CORES) diff --git a/cmake/modules/FindANTLR.cmake b/cmake/modules/FindANTLR.cmake new file mode 100644 index 0000000000..3fa497e8d4 --- /dev/null +++ b/cmake/modules/FindANTLR.cmake @@ -0,0 +1,174 @@ +find_package(Java QUIET COMPONENTS Runtime) + +# Find ANTLR (antlr-runtime/include, libantlr-runtime.a, antlr.jar) +# This module defines: +# ANTLR_JAR_LOCATION, directory containing jar +# ANTLR_INCLUDE_DIR, directory containing headers +# ANTLR_LIBRARY, path to static library +# +# this might fail +# https://gitlab.kitware.com/cmake/cmake/issues/19120 + +# ANTLR JAR HANDLING +if(NOT ANTLR_JAR_LOCATION) + find_file(ANTLR_JAR_LOCATION + NAMES antlr-4.12.0-complete.jar antlr.jar antlr4.jar antlr-4.jar + HINTS /usr /usr/local /usr/local/lib/ /usr/share /usr/share/java /usr/local/Homebrew ~/homebrew /usr/local/homebrew/Cellar /opt/homebrew/Cellar + PATH_SUFFIXES antlr 4.12.0 antlr/4.12.0 + NO_CMAKE_SYSTEM_PATH + NO_SYSTEM_ENVIRONMENT_PATH) + message("ANTLR Jar: ${ANTLR_JAR_LOCATION}") +endif() + +if(ANTLR_JAR_LOCATION AND Java_JAVA_EXECUTABLE) + execute_process( + COMMAND ${Java_JAVA_EXECUTABLE} -jar ${ANTLR_JAR_LOCATION} + OUTPUT_VARIABLE ANTLR_COMMAND_OUTPUT + ERROR_VARIABLE ANTLR_COMMAND_ERROR + RESULT_VARIABLE ANTLR_COMMAND_RESULT + OUTPUT_STRIP_TRAILING_WHITESPACE) + + if(ANTLR_COMMAND_RESULT EQUAL 0) + string(REGEX MATCH "Version [0-9]+(\\.[0-9]+)*" ANTLR_VERSION ${ANTLR_COMMAND_OUTPUT}) + string(REPLACE "Version " "" ANTLR_VERSION ${ANTLR_VERSION}) + else() + message( + SEND_ERROR + "Command '${Java_JAVA_EXECUTABLE} -jar ${ANTLR_JAR_LOCATION}' " + "failed with the output '${ANTLR_COMMAND_ERROR}'") + endif() + + macro(ANTLR_TARGET Name InputFile) + set(ANTLR_OPTIONS LEXER PARSER LISTENER VISITOR) + set(ANTLR_ONE_VALUE_ARGS PACKAGE OUTPUT_DIRECTORY DEPENDS_ANTLR) + set(ANTLR_MULTI_VALUE_ARGS COMPILE_FLAGS DEPENDS) + cmake_parse_arguments(ANTLR_TARGET + "${ANTLR_OPTIONS}" + "${ANTLR_ONE_VALUE_ARGS}" + "${ANTLR_MULTI_VALUE_ARGS}" + ${ARGN}) + + set(ANTLR_${Name}_INPUT ${InputFile}) + + get_filename_component(ANTLR_INPUT ${InputFile} NAME_WE) + + if(ANTLR_TARGET_OUTPUT_DIRECTORY) + set(ANTLR_${Name}_OUTPUT_DIR ${ANTLR_TARGET_OUTPUT_DIRECTORY}) + else() + set(ANTLR_${Name}_OUTPUT_DIR + ${CMAKE_CURRENT_BINARY_DIR}/antlr4cpp_generated_src/${ANTLR_INPUT}) + endif() + + unset(ANTLR_${Name}_CXX_OUTPUTS) + + if((ANTLR_TARGET_LEXER AND NOT ANTLR_TARGET_PARSER) OR + (ANTLR_TARGET_PARSER AND NOT ANTLR_TARGET_LEXER)) + list(APPEND ANTLR_${Name}_CXX_OUTPUTS + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}.h + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}.cpp) + set(ANTLR_${Name}_OUTPUTS + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}.interp + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}.tokens) + else() + list(APPEND ANTLR_${Name}_CXX_OUTPUTS + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}Lexer.h + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}Lexer.cpp + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}Parser.h + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}Parser.cpp) + list(APPEND ANTLR_${Name}_OUTPUTS + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}Lexer.interp + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}Lexer.tokens) + endif() + + if(ANTLR_TARGET_LISTENER) + list(APPEND ANTLR_${Name}_CXX_OUTPUTS + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}BaseListener.h + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}BaseListener.cpp + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}Listener.h + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}Listener.cpp) + list(APPEND ANTLR_TARGET_COMPILE_FLAGS -listener) + endif() + + if(ANTLR_TARGET_VISITOR) + list(APPEND ANTLR_${Name}_CXX_OUTPUTS + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}BaseVisitor.h + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}BaseVisitor.cpp + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}Visitor.h + ${ANTLR_${Name}_OUTPUT_DIR}/${ANTLR_INPUT}Visitor.cpp) + list(APPEND ANTLR_TARGET_COMPILE_FLAGS -visitor) + endif() + + if(ANTLR_TARGET_PACKAGE) + list(APPEND ANTLR_TARGET_COMPILE_FLAGS -package ${ANTLR_TARGET_PACKAGE}) + endif() + + list(APPEND ANTLR_${Name}_OUTPUTS ${ANTLR_${Name}_CXX_OUTPUTS}) + + if(ANTLR_TARGET_DEPENDS_ANTLR) + if(ANTLR_${ANTLR_TARGET_DEPENDS_ANTLR}_INPUT) + list(APPEND ANTLR_TARGET_DEPENDS + ${ANTLR_${ANTLR_TARGET_DEPENDS_ANTLR}_INPUT}) + list(APPEND ANTLR_TARGET_DEPENDS + ${ANTLR_${ANTLR_TARGET_DEPENDS_ANTLR}_OUTPUTS}) + else() + message(SEND_ERROR + "ANTLR target '${ANTLR_TARGET_DEPENDS_ANTLR}' not found") + endif() + endif() + + add_custom_command( + OUTPUT ${ANTLR_${Name}_OUTPUTS} + COMMAND ${Java_JAVA_EXECUTABLE} -jar ${ANTLR_JAR_LOCATION} + ${InputFile} + -o ${ANTLR_${Name}_OUTPUT_DIR} + -no-listener + -Dlanguage=Cpp + ${ANTLR_TARGET_COMPILE_FLAGS} + DEPENDS ${InputFile} + ${ANTLR_TARGET_DEPENDS} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Building ${Name} with ANTLR ${ANTLR_VERSION}") + endmacro(ANTLR_TARGET) + +endif(ANTLR_JAR_LOCATION AND Java_JAVA_EXECUTABLE) + +# ANTLR INCLUDE AND LIB +find_path(ANTLR_INCLUDE_DIR antlr4-runtime.h + PATHS ${ANTLR_ROOT}/include + HINTS /usr /usr/include /usr/local /usr/local/include /usr/local/Homebrew /usr/local/Homebrew/include ~/homebrew ~/homebrew/include /opt/homebrew /opt/homebrew/include + PATH_SUFFIXES antlr-runtime antlr4-runtime + NO_CMAKE_SYSTEM_PATH + NO_SYSTEM_ENVIRONMENT_PATH) + +find_path(ANTLR_LIB_DIR + NAMES libantlr4-runtime.a libantlr-runtime.a antlr_static.a antlr-static.a libantlr4-runtimeo.so libantlr-runtime.so antlr4.so antlr.so libantlr4-runtime.dylib libantlr-runtime.dylb antlr4.dylib antlr.dylib + PATHS ${ANTLR_ROOT}/lib + HINTS /usr /usr/local /usr/local/Homebrew ~/homebrew/ /opt/homebrew/ + PATH_SUFFIXES lib antlr-runtime antlr-runtime/lib lib/antlr-runtime + NO_CMAKE_SYSTEM_PATH + NO_SYSTEM_ENVIRONMENT_PATH) + +if(BUILD_SHARED_LIBS) + find_file(ANTLR_LIBRARY + NAMES libantlr4-runtime.so libantlr-runtime.so antlr4.so antlr.so libantlr4-runtime.dylib libantlr-runtime.dylb antlr4.dylib antlr.dylib + PATHS ${ANTLR_LIB_DIR} + HINTS /usr /usr/local /usr/share /user/share/java /usr/local/Homebrew ~/homebrew/ /opt/homebrew/ + PATH_SUFFIXES lib antlr-runtime antlr-runtime/lib lib/antlr-runtime + NO_CMAKE_SYSTEM_PATH + NO_SYSTEM_ENVIRONMENT_PATH) +else() + find_file(ANTLR_LIBRARY + NAMES libantlr4-runtime.a libantlr-runtime.a antlr_static.a antlr-static.a + PATHS ${ANTLR_LIB_DIR} + HINTS /usr /usr/local /usr/share /usr/share/java /usr/local/Homebrew ~/homebrew/ /opt/homebrew/ + PATH_SUFFIXES lib antlr-runtime antlr-runtime/lib lib/antlr-runtime + NO_CMAKE_SYSTEM_PATH + NO_SYSTEM_ENVIRONMENT_PATH) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args( + ANTLR + REQUIRED_VARS ANTLR_JAR_LOCATION Java_JAVA_EXECUTABLE ANTLR_INCLUDE_DIR ANTLR_LIBRARY + VERSION_VAR ANTLR_VERSION) + diff --git a/cmake/modules/FindUHDM.cmake b/cmake/modules/FindUHDM.cmake new file mode 100644 index 0000000000..ccd5671f2b --- /dev/null +++ b/cmake/modules/FindUHDM.cmake @@ -0,0 +1,48 @@ +cmake_minimum_required(VERSION 3.7.2) + +# Find UHDM (uhdm/include, libuhdm.a) +# This module defines: +# UHDM_INCLUDE_DIR, directory containing headers +# UHDM_LIBRARY, path to library (either static or shared) +# +# this might fail +# https://gitlab.kitware.com/cmake/cmake/issues/19120 + +find_path(UHDM_INCLUDE_DIRS uhdm/uhdm.h + PATHS ${UHDM_ROOT}/include + HINTS /usr /usr/local /usr/local/Homebrew ~/homebrew/ /opt/homebrew/ + PATH_SUFFIXES include uhdm uhdm/include include/uhdm + NO_CMAKE_SYSTEM_PATH + NO_SYSTEM_ENVIRONMENT_PATH) + +find_path(UHDM_LIB_DIR + NAMES libUHDM.a libuhdm.a libUHDM.so libuhdm.so libUHDM.dylib libuhdm.dylib + PATHS ${UHDM_ROOT}/lib + HINTS /usr /usr/local /usr/local/Homebrew ~/homebrew/ /opt/homebrew/ + PATH_SUFFIXES lib uhdm uhdm/lib lib/uhdm + NO_CMAKE_SYSTEM_PATH + NO_SYSTEM_ENVIRONMENT_PATH) + +if(BUILD_SHARED_LIBS) + find_file(UHDM_LIBRARY + NAMES libUHDM.so libuhdm.so libUHDM.dylib libuhdm.dylib + PATHS ${UHDM_LIB_DIR} + HINTS /usr /usr/local /usr/local/Homebrew ~/homebrew/ /opt/homebrew/ + PATH_SUFFIXES lib uhdm uhdm/lib lib/uhdm + NO_CMAKE_SYSTEM_PATH + NO_SYSTEM_ENVIRONMENT_PATH) +else() + find_file(UHDM_LIBRARY + NAMES libUHDM.a libuhdm.a + PATHS ${UHDM_LIB_DIR} + HINTS /usr /usr/local /usr/local/Homebrew ~/homebrew/ /opt/homebrew/ + PATH_SUFFIXES lib uhdm uhdm/lib lib/uhdm + NO_CMAKE_SYSTEM_PATH + NO_SYSTEM_ENVIRONMENT_PATH) +endif() + + +include(FindPackageHandleStandardArgs) + +find_package_handle_standard_args(UHDM REQUIRED_VARS UHDM_INCLUDE_DIRS UHDM_LIBRARY) + diff --git a/tests/TestInstall/CMakeLists.txt b/tests/TestInstall/CMakeLists.txt index c61deaee0f..d533d15ffe 100644 --- a/tests/TestInstall/CMakeLists.txt +++ b/tests/TestInstall/CMakeLists.txt @@ -21,13 +21,42 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../../cmake/modules" ${CMAKE_MODULE_PATH}) + +# Dependencies +if(SURELOG_USE_HOST_FLATBUFFERS) + find_package(Flatbuffers REQUIRED) + set(FLATBUFFERS_LIBRARY flatbuffers::flatbuffers) +else() + set(FLATBUFFERS_LIBRARY flatbuffers) +endif() + +if(SURELOG_USE_HOST_ANTLR) + find_package(ANTLR REQUIRED) +else() + set(ANTLR_LIBRARY antlr4-runtime$<$:-static>) +endif() + +if(SURELOG_USE_HOST_UHDM) + find_package(UHDM REQUIRED) + find_package(CapnProto) + set(CAPNPROTO_LIBRARY CapnProto::capnp) +else() + set(UHDM_LIBRARY uhdm) + set(CAPNPROTO_LIBRARY capnp kj) +endif() + +if(SURELOG_USE_HOST_GTEST) + find_package(GTest REQUIRED) +endif() + # Python if (SURELOG_WITH_PYTHON) -find_package(Python3 3.3 REQUIRED COMPONENTS Interpreter Development) -message(STATUS "Python3_LIBRARIES = ${Python3_LIBRARIES}") -message(STATUS "Python3_EXECUTABLE = ${Python3_EXECUTABLE}") -message(STATUS "Python3_INCLUDE_DIRS = ${Python3_INCLUDE_DIRS}") -message(STATUS "Python3_RUNTIME_LIBRARY_DIRS = ${Python3_RUNTIME_LIBRARY_DIRS}") + find_package(Python3 3.3 REQUIRED COMPONENTS Interpreter Development) + message(STATUS "Python3_LIBRARIES = ${Python3_LIBRARIES}") + message(STATUS "Python3_EXECUTABLE = ${Python3_EXECUTABLE}") + message(STATUS "Python3_INCLUDE_DIRS = ${Python3_INCLUDE_DIRS}") + message(STATUS "Python3_RUNTIME_LIBRARY_DIRS = ${Python3_RUNTIME_LIBRARY_DIRS}") endif() if(NOT NO_TCMALLOC) @@ -53,7 +82,7 @@ endif() add_executable(test_hellosureworld ${PROJECT_SOURCE_DIR}/../../src/hellosureworld.cpp) target_include_directories(test_hellosureworld PRIVATE - ${INSTALL_DIR}/include/surelog + ${INSTALL_DIR}/include/Surelog ${INSTALL_DIR}/include ${Python3_INCLUDE_DIRS} ) @@ -65,22 +94,20 @@ target_link_directories(test_hellosureworld if (SURELOG_WITH_PYTHON) target_link_libraries(test_hellosureworld surelog - antlr4-runtime$<$:-static> - flatbuffers - uhdm - capnp - kj + ${ANTLR_LIBRARY} + ${FLATBUFFERS_LIBRARY} + ${UHDM_LIBRARY} + ${CAPNPROTO_LIBRARY} ${Python3_LIBRARIES} $<$:tcmalloc> ) else() target_link_libraries(test_hellosureworld surelog - antlr4-runtime$<$:-static> - flatbuffers - uhdm - capnp - kj + ${ANTLR_LIBRARY} + ${FLATBUFFERS_LIBRARY} + ${UHDM_LIBRARY} + ${CAPNPROTO_LIBRARY} $<$:tcmalloc> ) endif()