From 5747c45ab5a81b9c493d00d047db5567a1c4acc8 Mon Sep 17 00:00:00 2001 From: Alexey Fedoseev Date: Thu, 9 May 2024 23:09:07 +0300 Subject: [PATCH] Change build system to CMake --- CMakeLists.txt | 82 +++++++++++++++++++++++++++++++++++ Makefile => Makefile.old | 0 README.md | 13 +++--- cyberiadamlpp-config.cmake.in | 8 ++++ cyberiadamlpp.h | 2 +- run-tests.sh | 4 +- 6 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 CMakeLists.txt rename Makefile => Makefile.old (100%) create mode 100644 cyberiadamlpp-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..757d8df --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,82 @@ +cmake_minimum_required(VERSION 3.21) + +project(cyberiadamlpp VERSION 1.0) + +find_package(LibXml2 REQUIRED) +if(!LibXml2_FOUND) + message(FATAL_ERROR "Cannot find libxml2 library") +endif() + +set(cyberiadaml_DIR /usr/lib/cmake) +find_package(cyberiadaml REQUIRED) +if(!cyberiadaml_FOUND) + message(FATAL_ERROR "Cannot find libcyberiadaml library") +endif() + +add_library(cyberiadamlpp SHARED cyberiadamlpp.cpp) +target_include_directories(cyberiadamlpp PUBLIC + $ + $ + "${cyberiadaml_INCLUDE_DIRS}") + +target_link_directories(cyberiadamlpp PUBLIC "${cyberiadaml_LIBRARY}") +target_link_libraries(cyberiadamlpp PUBLIC "${cyberiadaml_LIBRARIES}") + +add_executable(cyberiadapp main.cpp) +target_include_directories(cyberiadapp PUBLIC + $ + $) +target_link_directories(cyberiadapp PUBLIC "${PROJECT_BINARY_DIR}") +target_link_libraries(cyberiadapp PUBLIC cyberiadamlpp) + +file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/tests/") +file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp") +foreach(source_path ${files}) + get_filename_component(target_name "${source_path}" NAME_WE) + string(CONCAT target_name "${target_name}" ".test") + add_executable("${target_name}" "${source_path}") + target_include_directories("${target_name}" PUBLIC $) + target_link_directories("${target_name}" PUBLIC "${PROJECT_BINARY_DIR}") + target_link_libraries("${target_name}" PUBLIC cyberiadamlpp) + if (WIN32) + add_custom_command(TARGET "${target_name}" + POST_BUILD + COMMAND copy "${PROJECT_BINARY_DIR}/${target_name}" "${PROJECT_BINARY_DIR}/tests/" + ) + else() + add_custom_command(TARGET "${target_name}" + POST_BUILD + COMMAND ln -s "${PROJECT_BINARY_DIR}/${target_name}" "${PROJECT_BINARY_DIR}/tests/" + ) + endif() +endforeach() + +file(GLOB test_files + "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.graphml" + "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.txt" + ) +foreach(f ${test_files}) + get_filename_component(fname "${f}" NAME) + file(COPY_FILE + "${f}" + "${PROJECT_BINARY_DIR}/tests/${fname}" + ONLY_IF_DIFFERENT) +endforeach() + +file(COPY_FILE + ${PROJECT_SOURCE_DIR}/run-tests.sh + ${PROJECT_BINARY_DIR}/run-tests.sh ONLY_IF_DIFFERENT) + +install(TARGETS cyberiadamlpp DESTINATION lib EXPORT cyberiadamlpp) +install(FILES cyberiadamlpp.h + ${CMAKE_CURRENT_SOURCE_DIR}/cyberiadamlpp.h + DESTINATION include/cyberiada) +install(EXPORT cyberiadamlpp DESTINATION lib/cmake) + +configure_file( + ${PROJECT_SOURCE_DIR}/cyberiadamlpp-config.cmake.in + ${PROJECT_BINARY_DIR}/cyberiadamlpp-config.cmake @ONLY) + +install(FILES + ${PROJECT_BINARY_DIR}/cyberiadamlpp-config.cmake + DESTINATION lib/cmake) diff --git a/Makefile b/Makefile.old similarity index 100% rename from Makefile rename to Makefile.old diff --git a/README.md b/README.md index 203474c..8b441fb 100644 --- a/README.md +++ b/README.md @@ -12,17 +12,20 @@ the GNU Free Documentation License (version 1.3). * libcyberidaml * libstdc++ +* cmake (version 3.12+) ## Installation -Run `make` to build the library binaries. +Create `build` directory: `mkdir build && cd build` -Run `make main` to build the console printer/converter program. +Run `cmake ..` to build the library binaries and the test program. -Use variables: -* `DEBUG=1` debug version of the library -* `DYNAMIC=1` build shared version of the library +Run `make install` to install the library. + +Use CMake parameters to change the build type / installation prefix / etc. ## Testing +`cd build` + Run `run-tests.sh` to build and process the tests. diff --git a/cyberiadamlpp-config.cmake.in b/cyberiadamlpp-config.cmake.in new file mode 100644 index 0000000..c704765 --- /dev/null +++ b/cyberiadamlpp-config.cmake.in @@ -0,0 +1,8 @@ +get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH) +get_filename_component(_prefix "${_dir}/../.." ABSOLUTE) + +include("${_prefix}/lib/cmake/cyberiadamlpp.cmake") + +set(cyberiadamlpp_INCLUDE_DIRS "${_prefix}/include/") +set(cyberiadamlpp_LIBRARY "${_prefix}/lib/") +set(cyberiadamlpp_LIBRARIES "cyberiadamlpp") diff --git a/cyberiadamlpp.h b/cyberiadamlpp.h index f8237fc..ba2a2f7 100644 --- a/cyberiadamlpp.h +++ b/cyberiadamlpp.h @@ -27,7 +27,7 @@ #include #include #include -#include +#include // ----------------------------------------------------------------------------- // The Cyberiada GraphML classes diff --git a/run-tests.sh b/run-tests.sh index 6cfe800..e993d28 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -1,7 +1,7 @@ #!/bin/bash -DEBUG=1 make -DEBUG=1 make tests +cmake -DCMAKE_BUILD_TYPE=Debug .. +make if [ $? != 0 ] then echo "make test failed!"