Commit 5f33d9330808e91234ee2b9e2820910ebc595511

Lisandro Damián Nicanor Pérez Meyer 2019-02-07T15:54:25

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

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}