diff cmake/MolkoDefineLibrary.cmake @ 118:3411daa26432

cmake: create macros for building assets, continue #2487 @1h
author David Demelier <markand@malikania.fr>
date Sun, 04 Oct 2020 22:00:20 +0200
parents
children 43e04bf2c350
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake/MolkoDefineLibrary.cmake	Sun Oct 04 22:00:20 2020 +0200
@@ -0,0 +1,99 @@
+#
+# MolkoDefineLibrary.cmake -- CMake build system for molko
+#
+# Copyright (c) 2020 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.
+#
+
+#
+# molko_define_library
+# --------------------
+#
+# Synopsis:
+#
+# molko_define_library(
+#   TARGET              target name
+#   SOURCES             src1, src2, srcn
+#   ASSETS              (Optional) list of assets
+#   LIBRARIES           (Optional) libraries to link
+#   PRIVATE_FLAGS       (Optional) C flags (without -D)
+#   PRIVATE_INCLUDES    (Optional) local includes for the target only
+#   PUBLIC_FLAGS        (Optional) C flags (without -D)
+#   PUBLIC_INCLUDES     (Optional) includes to share with target dependencies
+# )
+#
+# Create a library and optionally install it.
+#
+# The function create a new library named with the parameter TARGET, you should
+# prefix it with "lib" as its the convention within molko (e.g. libfoo), the
+# prefix is automatically removed.
+#
+# The argument SOURCES should contains the C source files and HEADERS should
+# points to a directory to be installed verbatim in the include directory.
+#
+# Optional argument PRIVATE_FLAGS, PUBLIC_FLAGS, PRIVATE_INCLUDES,
+# PUBLIC_INCLUDES, LIBRARIES may be passed to set compile flags, includes and
+# libraries respectively.
+#
+# The arguments ASSETS contains a list of assets to be converted during the
+# build. The file hierarchy is conserved in the ${CMAKE_CURRENT_BINARY_DIR}.
+#
+# If EXPORT boolean parameter is set, the library is exported and installed.
+#
+
+include(${CMAKE_CURRENT_LIST_DIR}/MolkoBuildAssets.cmake)
+
+function(molko_define_library)
+	set(options)
+	set(oneValueArgs TARGET)
+	set(multiValueArgs ASSETS LIBRARIES PRIVATE_FLAGS PRIVATE_INCLUDES PUBLIC_FLAGS PUBLIC_INCLUDES SOURCES)
+
+	cmake_parse_arguments(LIB "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
+
+	if (NOT LIB_TARGET)
+		message(FATAL_ERROR "Missing TARGET argument")
+	endif ()
+	if (NOT LIB_SOURCES)
+		message(FATAL_ERROR "Missing SOURCES argument")
+	endif ()
+
+	molko_build_assets("${LIB_ASSETS}" OUTPUTS)
+
+	add_library(${LIB_TARGET} ${LIB_SOURCES} ${OUTPUTS})
+	target_include_directories(
+		${LIB_TARGET}
+		PRIVATE
+			${LIB_PRIVATE_INCLUDES}
+		PUBLIC
+			$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
+			${LIB_PUBLIC_INCLUDES}
+	)
+	target_compile_definitions(
+		${LIB_TARGET}
+		PRIVATE
+			${LIB_PRIVATE_FLAGS}
+		PUBLIC
+			${LIB_PUBLIC_FLAGS}
+	)
+	target_link_libraries(${LIB_TARGET} ${LIB_LIBRARIES})
+	set_target_properties(
+		${LIB_TARGET}
+		PROPERTIES
+			PREFIX ""
+			IMPORT_PREFIX ""
+			C_EXTENSIONS Off
+			C_STANDARD 11
+			C_STANDARD_REQUIRED On
+	)
+endfunction()