Change build system to CMake

This commit is contained in:
Alexey Fedoseev
2024-05-09 23:09:07 +03:00
parent 510a3ddea9
commit 5747c45ab5
6 changed files with 101 additions and 8 deletions

82
CMakeLists.txt Normal file
View File

@@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include/cyberiada>
"${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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include/cyberiada>)
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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
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)

View File

@@ -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.

View File

@@ -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")

View File

@@ -27,7 +27,7 @@
#include <list>
#include <vector>
#include <ostream>
#include <cyberiadaml.h>
#include <cyberiada/cyberiadaml.h>
// -----------------------------------------------------------------------------
// The Cyberiada GraphML classes

View File

@@ -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!"