diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..190b5da0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,75 @@ +cmake_minimum_required(VERSION 3.10) + +project(NanoSVG C) + +# CMake needs *.c files to do something useful +configure_file(src/nanosvg.h ${CMAKE_CURRENT_BINARY_DIR}/nanosvg.c) +configure_file(src/nanosvgrast.h ${CMAKE_CURRENT_BINARY_DIR}/nanosvgrast.c) + +add_library(nanosvg ${CMAKE_CURRENT_BINARY_DIR}/nanosvg.c) + +find_library(MATH_LIBRARY m) # Business as usual +if(MATH_LIBRARY) + target_link_libraries(nanosvg PUBLIC ${MATH_LIBRARY}) +endif() + +target_include_directories(nanosvg PUBLIC $) +target_compile_definitions(nanosvg PRIVATE NANOSVG_IMPLEMENTATION) + +# Same for nanosvgrast +add_library(nanosvgrast ${CMAKE_CURRENT_BINARY_DIR}/nanosvgrast.c) +target_link_libraries(nanosvgrast PUBLIC nanosvg) +target_include_directories(nanosvgrast PRIVATE src) +target_compile_definitions(nanosvgrast PRIVATE NANOSVGRAST_IMPLEMENTATION) + +# Installation and export: + +include(CMakePackageConfigHelpers) + +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + VERSION 1.0 + COMPATIBILITY AnyNewerVersion +) + +install(TARGETS nanosvg nanosvgrast + EXPORT ${PROJECT_NAME}Targets +) + +export(EXPORT ${PROJECT_NAME}Targets + FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake" + NAMESPACE ${PROJECT_NAME}:: +) + +set(ConfigPackageLocation lib/cmake/${PROJECT_NAME}) + +configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + INSTALL_DESTINATION ${ConfigPackageLocation} + NO_CHECK_REQUIRED_COMPONENTS_MACRO +) + +install( + FILES + src/nanosvg.h + src/nanosvgrast.h + DESTINATION + include/nanosvg + ) + +install(EXPORT ${PROJECT_NAME}Targets + FILE + ${PROJECT_NAME}Targets.cmake + NAMESPACE + ${PROJECT_NAME}:: + DESTINATION + ${ConfigPackageLocation} +) + +install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + DESTINATION + ${ConfigPackageLocation} +) \ No newline at end of file diff --git a/Config.cmake.in b/Config.cmake.in new file mode 100644 index 00000000..9311d80d --- /dev/null +++ b/Config.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ + +if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/NanoSVGTargets.cmake) + include("${CMAKE_CURRENT_LIST_DIR}/NanoSVGTargets.cmake") +endif () \ No newline at end of file diff --git a/README.md b/README.md index 0865cbe2..b468fba2 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,16 @@ By default, NanoSVG parses only the most common colors. In order to get support #include "nanosvg.h" ``` +Alternatively, you can install the library using CMake and import it into your project using the standard CMake `find_package` command. + +```CMake +add_executable(myexe main.c) + +find_package(NanoSVG REQUIRED) + +target_link_libraries(myexe NanoSVG::nanosvg NanoSVG::nanosvgrast) +``` + ## Compiling Example Project In order to compile the demo project, your will need to install [GLFW](http://www.glfw.org/) to compile. diff --git a/src/nanosvg.h b/src/nanosvg.h index 5077f275..0c586161 100644 --- a/src/nanosvg.h +++ b/src/nanosvg.h @@ -187,6 +187,7 @@ void nsvgDelete(NSVGimage* image); #include #include +#include #include #define NSVG_PI (3.14159265358979323846264338327f) diff --git a/src/nanosvgrast.h b/src/nanosvgrast.h index b740c316..cd91226e 100644 --- a/src/nanosvgrast.h +++ b/src/nanosvgrast.h @@ -25,6 +25,8 @@ #ifndef NANOSVGRAST_H #define NANOSVGRAST_H +#include "nanosvg.h" + #ifndef NANOSVGRAST_CPLUSPLUS #ifdef __cplusplus extern "C" { @@ -77,6 +79,8 @@ void nsvgDeleteRasterizer(NSVGrasterizer*); #ifdef NANOSVGRAST_IMPLEMENTATION #include +#include +#include #define NSVG__SUBSAMPLES 5 #define NSVG__FIXSHIFT 10