Build md4c as a shared library. (#49) Build md4c as a shared library. - Define the current version in the main CMakeLists.txt, so it can be used within the project. - Define VERSION, SOVERSION and PUBLIC_HEADER as target properties. - Be able to install both libmd4c and md2html. - Create a pkg-config file. Fixes #48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a62798f..5239802 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,7 +1,17 @@
-cmake_minimum_required(VERSION 2.8)
+cmake_minimum_required(VERSION 3.4)
project(MD4C C)
+set(MD_VERSION_MAJOR 0)
+set(MD_VERSION_MINOR 2)
+set(MD_VERSION_RELEASE 7)
+set(MD_VERSION "${MD_VERSION_MAJOR}.${MD_VERSION_MINOR}.${MD_VERSION_RELEASE}")
+
+add_definitions(
+ -DMD_VERSION_MAJOR=${MD_VERSION_MAJOR}
+ -DMD_VERSION_MINOR=${MD_VERSION_MINOR}
+ -DMD_VERSION_RELEASE=${MD_VERSION_RELEASE}
+)
set(CMAKE_CONFIGURATION_TYPES Debug Release RelWithDebInfo MinSizeRel)
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
@@ -26,6 +36,7 @@ elseif(MSVC)
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_RELEASE} /MT")
endif()
+include(GNUInstallDirs)
add_subdirectory(md4c)
add_subdirectory(md2html)
diff --git a/README.md b/README.md
index 3c95d66..b4d95c9 100644
--- a/README.md
+++ b/README.md
@@ -55,6 +55,10 @@ MD4C is C Markdown parser with the following features:
[Cmark](https://github.com/jgm/cmark).
+## Building MD4C as a shared library
+
+Be sure to define BUILD_SHARED_LIBS when building.
+
## Using MD4C
The parser is implemented in a single C source file `md4c.c` and its
diff --git a/appveyor.yml b/appveyor.yml
index 995a51d..a254b4f 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -10,7 +10,7 @@ before_build:
build:
project: md4c.sln
- verbosity: minimal
+ verbosity: detailed
skip_tags: true
diff --git a/md2html/CMakeLists.txt b/md2html/CMakeLists.txt
index a873312..b7bdee2 100644
--- a/md2html/CMakeLists.txt
+++ b/md2html/CMakeLists.txt
@@ -3,3 +3,5 @@ include_directories("${PROJECT_SOURCE_DIR}/md4c")
add_executable(md2html cmdline.c cmdline.h entity.c entity.h md2html.c render_html.c render_html.h)
target_link_libraries(md2html md4c)
+
+install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/md2html DESTINATION ${CMAKE_INSTALL_BINDIR})
diff --git a/md4c/CMakeLists.txt b/md4c/CMakeLists.txt
index f9577a3..8fc846f 100644
--- a/md4c/CMakeLists.txt
+++ b/md4c/CMakeLists.txt
@@ -1,6 +1,33 @@
+# Be sure to export all symbols in Windows.
+set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS 1)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")
add_definitions(-DMD4C_USE_UTF8)
-add_library(md4c STATIC md4c.c md4c.h)
+set(md4c_src
+ md4c.c
+)
+
+add_library(md4c SHARED ${md4c_src})
+
+set_target_properties(md4c PROPERTIES
+ VERSION ${MD_VERSION}
+ SOVERSION ${MD_VERSION_MAJOR}
+ PUBLIC_HEADER md4c.h
+)
+
+install(
+ TARGETS md4c
+ EXPORT md4cConfig
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
+)
+
+# Create a pkg-config file
+configure_file(md4c.pc.in md4c.pc @ONLY)
+install(FILES ${CMAKE_BINARY_DIR}/md4c/md4c.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
+
+# And a CMake file
+install(EXPORT md4cConfig DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/md4c/)
diff --git a/md4c/md4c.h b/md4c/md4c.h
index 0cb2b87..7caba5d 100644
--- a/md4c/md4c.h
+++ b/md4c/md4c.h
@@ -30,12 +30,6 @@
extern "C" {
#endif
-
-#define MD_VERSION_MAJOR 0
-#define MD_VERSION_MINOR 2
-#define MD_VERSION_RELEASE 7
-
-
/* Magic to support UTF-16. */
#if defined MD4C_USE_UTF16
#ifdef _WIN32
diff --git a/md4c/md4c.pc.in b/md4c/md4c.pc.in
new file mode 100644
index 0000000..61c78d8
--- /dev/null
+++ b/md4c/md4c.pc.in
@@ -0,0 +1,12 @@
+prefix=@CMAKE_INSTALL_PREFIX@
+exec_prefix=@CMAKE_INSTALL_PREFIX@
+libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
+includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
+
+Name: @PROJECT_NAME@
+Description: @PROJECT_DESCRIPTION@
+Version: @PROJECT_VERSION@
+
+Requires:
+Libs: -L${libdir} -lmd4c
+Cflags: -I${includedir}