Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMake build script to the project #209

Merged
merged 3 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<INSTALL_INTERFACE:include/nanosvg>)
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}
)
5 changes: 5 additions & 0 deletions Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@PACKAGE_INIT@

if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/NanoSVGTargets.cmake)
include("${CMAKE_CURRENT_LIST_DIR}/NanoSVGTargets.cmake")
endif ()
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/nanosvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ void nsvgDelete(NSVGimage* image);

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#define NSVG_PI (3.14159265358979323846264338327f)
Expand Down
4 changes: 4 additions & 0 deletions src/nanosvgrast.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef NANOSVGRAST_H
#define NANOSVGRAST_H

#include "nanosvg.h"
Copy link

@akien-mga akien-mga May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, it's no longer possible to include nanosvgrast.h after including nanosvg.h as it leads to duplicate symbols:

$ cat << EOF > nanosvg.cc
#include "stdio.h"
#include "string.h"
#include "math.h"
#define NANOSVG_ALL_COLOR_KEYWORDS
#define NANOSVG_IMPLEMENTATION
#include "nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvgrast.h"
EOF

$ g++ nanosvg.cc
In file included from nanosvgrast.h:28,
                 from nanosvg.cc:8:
nanosvg.h:219:12: error: redefinition of ‘int nsvg__isspace(char)’
  219 | static int nsvg__isspace(char c)
      |            ^~~~~~~~~~~~~
In file included from nanosvg.cc:6:
nanosvg.h:219:12: note: ‘int nsvg__isspace(char)’ previously defined here
  219 | static int nsvg__isspace(char c)
      |            ^~~~~~~~~~~~~
nanosvg.h:224:12: error: redefinition of ‘int nsvg__isdigit(char)’
  224 | static int nsvg__isdigit(char c)
      |            ^~~~~~~~~~~~~
nanosvg.h:224:12: note: ‘int nsvg__isdigit(char)’ previously defined here
  224 | static int nsvg__isdigit(char c)
      |            ^~~~~~~~~~~~~
nanosvg.h:229:26: error: redefinition of ‘float nsvg__minf(float, float)’
  229 | static NSVG_INLINE float nsvg__minf(float a, float b) { return a < b ? a : b; }
      |                          ^~~~~~~~~~
nanosvg.h:229:26: note: ‘float nsvg__minf(float, float)’ previously defined here
  229 | static NSVG_INLINE float nsvg__minf(float a, float b) { return a < b ? a : b; }
      |                          ^~~~~~~~~~
nanosvg.h:230:26: error: redefinition of ‘float nsvg__maxf(float, float)’
  230 | static NSVG_INLINE float nsvg__maxf(float a, float b) { return a > b ? a : b; }
      |                          ^~~~~~~~~~
nanosvg.h:230:26: note: ‘float nsvg__maxf(float, float)’ previously defined here
  230 | static NSVG_INLINE float nsvg__maxf(float a, float b) { return a > b ? a : b; }
      |                          ^~~~~~~~~~
...

and dozens more errors.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That include should not be there, my bad reviewing it. @tamasmeszaros would you mind making another PR fixing that?

Copy link
Contributor Author

@tamasmeszaros tamasmeszaros May 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I would not remove the include but rather extend the include guards for the whole files for nanosvg.h and nanosvgrast.h.

See #215
I've also added a bonus PR #216 with a CMake build script for the examples. Use it if you like it :)


#ifndef NANOSVGRAST_CPLUSPLUS
#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -77,6 +79,8 @@ void nsvgDeleteRasterizer(NSVGrasterizer*);
#ifdef NANOSVGRAST_IMPLEMENTATION

#include <math.h>
#include <stdlib.h>
#include <string.h>

#define NSVG__SUBSAMPLES 5
#define NSVG__FIXSHIFT 10
Expand Down