view CMakeLists.txt @ 345:c293dbe181c0

Zip: add exists() function Submitted by: Elias Pipping
author David Demelier <markand@malikania.fr>
date Tue, 31 Mar 2015 09:57:37 +0200
parents b32a0d29d97a
children 47a206e724f2
line wrap: on
line source

#
# CMakeLists.txt -- code building for common code
#
# Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

#
# This CMakeLists build system is primarily used for testing all
# modules.
#
cmake_minimum_required(VERSION 2.8.11)
project(code)

set(CMAKE_MODULE_PATH ${code_SOURCE_DIR}/cmake)

include(CMakeParseArguments)

add_subdirectory(extern)
add_subdirectory(tools)

enable_testing()

macro(define_module)
	set(oneValueArgs TARGET NAME DIRECTORY)
	set(multiValueArgs SOURCES RESOURCES DOCS LIBRARIES INCLUDES)

	cmake_parse_arguments(MOD "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

	if (NOT MOD_TARGET)
		message(FATAL_ERROR "Argument TARGET not set")
	elseif (NOT MOD_NAME)
		message(FATAL_ERROR "Argument NAME not set")
	elseif (NOT MOD_DIRECTORY)
		message(FATAL_ERROR "Argument DIRECTORY not set")
	endif ()

	string(TOUPPER ${MOD_NAME} optionname)

	# Create the option for enabling the test
	option(WITH_${optionname} "Enable ${MOD_NAME}" On)

	if (WITH_${optionname})
		# Add the test executable
		set(MAIN ${code_SOURCE_DIR}/C++/tests/${MOD_DIRECTORY}/main.cpp)

		if (NOT EXISTS ${MAIN})
			message(FATAL_ERROR "${MAIN} file does not exists")
		endif ()

		add_executable(${MOD_TARGET} ${MOD_SOURCES} ${MOD_RESOURCES} ${MOD_DOCS} ${MAIN})
		add_test(${MOD_TARGET}-test ${MOD_TARGET})
		target_include_directories(
			${MOD_TARGET}
			PRIVATE
				${code_SOURCE_DIR}/C++
				${code_SOURCE_DIR}/C++/modules/${MOD_DIRECTORY}
				${MOD_INCLUDES}
		)
		target_link_libraries(${MOD_TARGET} gtest ${MOD_LIBRARIES})

		# Copy optional resources
		if (MOD_RESOURCES)
			foreach (res ${MOD_RESOURCES})
				get_filename_component(inputname ${res} NAME)
				set(output ${CMAKE_BINARY_DIR}/${MOD_DIRECTORY}/${inputname})
				list(APPEND outputlist ${output})

				add_custom_command(
					OUTPUT ${output}
					COMMENT "Copying ${inputname}"
					DEPENDS ${res}
					COMMAND ${CMAKE_COMMAND} -E copy ${res} ${output}
				)
			endforeach()

			add_custom_target(${MOD_TARGET}-resources DEPENDS ${outputlist})
			add_dependencies(${MOD_TARGET} ${MOD_TARGET}-resources)
		endif ()

		# Generate documentation locally
		if (MOD_DOCS)
			foreach (doc ${MOD_DOCS})
				file(RELATIVE_PATH inputbase ${code_SOURCE_DIR}/C++/doc/${MOD_DIRECTORY} ${doc})
				string(REGEX REPLACE "^(.*)\\.md" "\\1.html" outputname ${inputbase})
				set(output ${CMAKE_BINARY_DIR}/doc/${MOD_DIRECTORY}/${outputname})

				pandoc(
					SOURCES ${doc}
					OUTPUT ${output}
					FROM markdown TO html5
					MAKE_DIRECTORY STANDALONE
					FILTER $<TARGET_FILE:mdtohtml>
				)

				list(APPEND docoutputlist ${output})
			endforeach ()

			add_custom_target(${MOD_TARGET}-doc DEPENDS ${docoutputlist})
			add_dependencies(${MOD_TARGET} ${MOD_TARGET}-doc)
		endif ()
	endif ()
endmacro()

find_package(Pandoc REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Jansson REQUIRED)

# ---------------------------------------------------------
# Base64
# ---------------------------------------------------------

define_module(
	TARGET base64
	NAME Base64
	DIRECTORY Base64
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Base64/Base64.cpp
		${code_SOURCE_DIR}/C++/modules/Base64/Base64.h
	DOCS
		${code_SOURCE_DIR}/C++/doc/Base64/Home.md
		${code_SOURCE_DIR}/C++/doc/Base64/class/Base64.md
		${code_SOURCE_DIR}/C++/doc/Base64/class/Base64/decode.md
		${code_SOURCE_DIR}/C++/doc/Base64/class/Base64/encode.md
		${code_SOURCE_DIR}/C++/doc/Base64/class/Base64/lookup.md
		${code_SOURCE_DIR}/C++/doc/Base64/class/Base64/rlookup.md
)

# ---------------------------------------------------------
# Converter
# ---------------------------------------------------------

# No tests yet

# ---------------------------------------------------------
# Directory
# ---------------------------------------------------------

define_module(
	TARGET directory
	NAME Directory
	DIRECTORY Directory
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Directory/Directory.cpp
		${code_SOURCE_DIR}/C++/modules/Directory/Directory.h
)

# ---------------------------------------------------------
# Driver
# ---------------------------------------------------------

# No tests yet

# ---------------------------------------------------------
# Dynlib
# ---------------------------------------------------------

if (WIN32)
	set(EXTENSION ".dll")
elseif (UNIX)
	set(EXTENSION ".so")
elseif (APPLE)
	set(EXTENSION ".dylib")
else ()
	message(FATAL_ERROR "Unsupported platform")
endif ()

define_module(
	TARGET dynlib
	NAME Dynlib
	DIRECTORY Dynlib
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Dynlib/Dynlib.cpp
		${code_SOURCE_DIR}/C++/modules/Dynlib/Dynlib.h
	DOCS
		${code_SOURCE_DIR}/C++/doc/Dynlib/Home.md
		${code_SOURCE_DIR}/C++/doc/Dynlib/class/Dynlib.md
		${code_SOURCE_DIR}/C++/doc/Dynlib/class/Dynlib/Constructor.md
		${code_SOURCE_DIR}/C++/doc/Dynlib/class/Dynlib/Destructor.md
		${code_SOURCE_DIR}/C++/doc/Dynlib/class/Dynlib/Policy.md
		${code_SOURCE_DIR}/C++/doc/Dynlib/class/Dynlib/sym.md
		${code_SOURCE_DIR}/C++/doc/Dynlib/macro/DYNLIB_EXPORT.md
)

if (CMAKE_SYSTEM_NAME MATCHES "Linux")
	target_link_libraries(dynlib dl)
endif ()

target_compile_definitions(dynlib PRIVATE EXTENSION=\"${EXTENSION}\")

add_library(dynlib-plugin MODULE ${code_SOURCE_DIR}/C++/tests/Dynlib/Plugin.cpp)
set_target_properties(dynlib-plugin PROPERTIES PREFIX "")
target_include_directories(dynlib-plugin PRIVATE ${code_SOURCE_DIR}/C++/modules/Dynlib)

# ---------------------------------------------------------
# Flags
# ---------------------------------------------------------

define_module(
	TARGET flags
	NAME Flags
	DIRECTORY Flags
	SOURCES ${code_SOURCE_DIR}/C++/modules/Flags/Flags.h
)

# ---------------------------------------------------------
# Hash
# ---------------------------------------------------------

define_module(
	TARGET hash
	NAME Hash
	DIRECTORY Hash
	LIBRARIES ${OPENSSL_LIBRARIES}
	INCLUDES ${OPENSSL_INCLUDE_DIR}
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Hash/Hash.cpp
		${code_SOURCE_DIR}/C++/modules/Hash/Hash.h
)

# ---------------------------------------------------------
# Ini
# ---------------------------------------------------------

if (WIN32)
	set(INI_LIBRARIES Shlwapi)
endif ()

define_module(
	TARGET ini
	NAME Ini
	DIRECTORY Ini
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Ini/Ini.cpp
		${code_SOURCE_DIR}/C++/modules/Ini/Ini.h
	LIBRARIES
		${INI_LIBRARIES}
	RESOURCES
		${code_SOURCE_DIR}/C++/tests/Ini/configs/compact.conf
		${code_SOURCE_DIR}/C++/tests/Ini/configs/error-badcomment.conf
		${code_SOURCE_DIR}/C++/tests/Ini/configs/error-badsection.conf
		${code_SOURCE_DIR}/C++/tests/Ini/configs/error-lineassigment.conf
		${code_SOURCE_DIR}/C++/tests/Ini/configs/error-nosection.conf
		${code_SOURCE_DIR}/C++/tests/Ini/configs/includes.conf
		${code_SOURCE_DIR}/C++/tests/Ini/configs/multi.conf
		${code_SOURCE_DIR}/C++/tests/Ini/configs/novalue.conf
		${code_SOURCE_DIR}/C++/tests/Ini/configs/simple.conf
		${code_SOURCE_DIR}/C++/tests/Ini/configs/tokens.conf
)

# ---------------------------------------------------------
# Json
# ---------------------------------------------------------

define_module(
	TARGET json
	NAME Json
	DIRECTORY Json
	INCLUDES ${Jansson_INCLUDE_DIRS}
	LIBRARIES ${Jansson_LIBRARIES}
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Json/Json.cpp
		${code_SOURCE_DIR}/C++/modules/Json/Json.h
	RESOURCES
		${code_SOURCE_DIR}/C++/tests/Json/data/array-all.json
		${code_SOURCE_DIR}/C++/tests/Json/data/array.json
		${code_SOURCE_DIR}/C++/tests/Json/data/object-all.json
		${code_SOURCE_DIR}/C++/tests/Json/data/object.json
		${code_SOURCE_DIR}/C++/tests/Json/data/simple.json
)

# ---------------------------------------------------------
# OptionParser
# ---------------------------------------------------------

define_module(
	TARGET optionparser
	NAME OptionParser
	DIRECTORY OptionParser
	SOURCES
		${code_SOURCE_DIR}/C++/modules/OptionParser/OptionParser.cpp
		${code_SOURCE_DIR}/C++/modules/OptionParser/OptionParser.h
)

# ---------------------------------------------------------
# Pack
# ---------------------------------------------------------

define_module(
	TARGET pack
	NAME Pack
	DIRECTORY Pack
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Pack/Pack.cpp
		${code_SOURCE_DIR}/C++/modules/Pack/Pack.h
)

# ---------------------------------------------------------
# Parser (DEPRECATED)
# ---------------------------------------------------------

define_module(
	TARGET parser
	NAME Parser
	DIRECTORY Parser
	RESOURCES
		${code_SOURCE_DIR}/C++/tests/Parser/configs/simple.conf
		${code_SOURCE_DIR}/C++/tests/Parser/configs/multi.conf
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Parser/Parser.cpp
		${code_SOURCE_DIR}/C++/modules/Parser/Parser.h
)

# ---------------------------------------------------------
# Sockets
# ---------------------------------------------------------

if (WIN32)
	set(SOCKET_LIBRARIES ws2_32)
endif ()

define_module(
	TARGET socket
	NAME Socket
	DIRECTORY Socket
	INCLUDES ${OPENSSL_INCLUDE_DIR}
	LIBRARIES
		${SOCKET_LIBRARIES}
		${OPENSSL_LIBRARIES}
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Socket/SocketAddress.cpp
		${code_SOURCE_DIR}/C++/modules/Socket/SocketAddress.h
		${code_SOURCE_DIR}/C++/modules/Socket/Socket.cpp
		${code_SOURCE_DIR}/C++/modules/Socket/Socket.h
		${code_SOURCE_DIR}/C++/modules/Socket/SocketListener.cpp
		${code_SOURCE_DIR}/C++/modules/Socket/SocketListener.h
		${code_SOURCE_DIR}/C++/modules/Socket/SocketSsl.cpp
		${code_SOURCE_DIR}/C++/modules/Socket/SocketSsl.h
		${code_SOURCE_DIR}/C++/modules/Socket/SocketTcp.cpp
		${code_SOURCE_DIR}/C++/modules/Socket/SocketTcp.h
		${code_SOURCE_DIR}/C++/modules/Socket/SocketUdp.cpp
		${code_SOURCE_DIR}/C++/modules/Socket/SocketUdp.h
)

# ---------------------------------------------------------
# Treenode
# ---------------------------------------------------------

define_module(
	TARGET treenode
	NAME Treenode
	DIRECTORY Treenode
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Treenode/TreeNode.h
)

# ---------------------------------------------------------
# Utf8
# ---------------------------------------------------------

define_module(
	TARGET utf8
	NAME Utf8
	DIRECTORY Utf8
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Utf8/Utf8.cpp
		${code_SOURCE_DIR}/C++/modules/Utf8/Utf8.h
)

# ---------------------------------------------------------
# Xdg
# ---------------------------------------------------------

if (UNIX)
	define_module(
		TARGET xdg
		NAME Xdg
		DIRECTORY Xdg

		SOURCES
			${code_SOURCE_DIR}/C++/modules/Xdg/Xdg.cpp
			${code_SOURCE_DIR}/C++/modules/Xdg/Xdg.h
	)
endif ()

# ---------------------------------------------------------
# Zip
# ---------------------------------------------------------

find_package(ZIP REQUIRED)

define_module(
	TARGET zip
	NAME Zip
	DIRECTORY Zip
	INCLUDES ${ZIP_INCLUDE_DIRS}
	LIBRARIES ${ZIP_LIBRARIES}
	RESOURCES
		${code_SOURCE_DIR}/C++/tests/Zip/data/data.txt
		${code_SOURCE_DIR}/C++/tests/Zip/data/stats.zip
	SOURCES
		${code_SOURCE_DIR}/C++/modules/Zip/ZipArchive.cpp
		${code_SOURCE_DIR}/C++/modules/Zip/ZipArchive.h
)