changeset 26:c87772137575

cmake: create define_executable macro
author David Demelier <markand@malikania.fr>
date Fri, 27 Apr 2018 10:20:02 +0200
parents c62216fdd8c8
children ce74602c837f
files CMakeLists.txt cmake/function/DefineExecutable.cmake dmenu-background/CMakeLists.txt dmenu-mpc/CMakeLists.txt
diffstat 4 files changed, 83 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Fri Apr 27 10:19:05 2018 +0200
+++ b/CMakeLists.txt	Fri Apr 27 10:20:02 2018 +0200
@@ -22,7 +22,10 @@
 set(CMAKE_CXX_STANDARD 14)
 set(CMAKE_CXX_STANDARD_REQUIRED On)
 
+include(cmake/function/DefineExecutable.cmake)
+
 find_package(Boost REQUIRED COMPONENTS filesystem system)
+find_package(PkgConfig REQUIRED)
 
 add_subdirectory(libdmenutools)
 add_subdirectory(dmenu-background)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake/function/DefineExecutable.cmake	Fri Apr 27 10:20:02 2018 +0200
@@ -0,0 +1,70 @@
+#
+# DefineExecutable.cmake -- CMake build system for dmenutools
+#
+# Copyright (c) 2018 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.
+#
+
+#
+# define_executable
+# -----------------------
+#
+# define_executable(
+#    NAME canonical name
+#    SOURCES src1, src2, srcn
+#    FLAGS (Optional) C/C++ flags (without -D)
+#    LIBRARIES (Optional) libraries to link
+#    INCLUDES (Optional) includes for the target
+# )
+#
+# Create an executable that will be installed. This function creates an option
+# WITH_$NAME where NAME has been set to uppercase.
+#
+# If the option is disabled, WITH_$NAME_MSG will be set to No, otherwise, it is
+# set to yes.
+#
+
+function(define_executable)
+    set(options "")
+    set(oneValueArgs NAME)
+    set(multiValueArgs SOURCES FLAGS LIBRARIES INCLUDES)
+
+    cmake_parse_arguments(EXE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
+
+    if (NOT EXE_NAME)
+        message(FATAL_ERROR "Please set NAME")
+    endif ()
+    if (NOT EXE_SOURCES)
+        message(FATAL_ERROR "Please set SOURCES")
+    endif ()
+
+    string(TOUPPER ${EXE_NAME} NAME)
+
+    option(WITH_${NAME} "Enable ${EXE_NAME} component" On)
+
+    if (NOT WITH_${NAME})
+        set(WITH_${NAME}_MSG "No (disabled by user)" CACHE INTERNAL "" FORCE)
+    else ()
+        set(WITH_${NAME}_MSG "Yes" CACHE INTERNAL "" FORCE)
+        add_executable(dmenu-${EXE_NAME} ${EXE_SOURCES})
+        target_include_directories(dmenu-${EXE_NAME} PRIVATE ${EXE_INCLUDES})
+        target_compile_definitions(dmenu-${EXE_NAME} PRIVATE ${EXE_FLAGS})
+        target_link_libraries(dmenu-${EXE_NAME} libdmenutools ${EXE_LIBRARIES})
+
+        install(
+            TARGETS dmenu-${EXE_NAME}
+            RUNTIME DESTINATION bin
+        )
+    endif ()
+endfunction()
--- a/dmenu-background/CMakeLists.txt	Fri Apr 27 10:19:05 2018 +0200
+++ b/dmenu-background/CMakeLists.txt	Fri Apr 27 10:20:02 2018 +0200
@@ -17,6 +17,8 @@
 #
 
 project(dmenu-background)
-add_executable(dmenu-background main.cpp)
-target_link_libraries(dmenu-background libdmenutools)
-set(WITH_BACKGROUND_MSG "Yes" CACHE "" INTERNAL FORCE)
+
+define_executable(
+    NAME background
+    SOURCES main.cpp
+)
--- a/dmenu-mpc/CMakeLists.txt	Fri Apr 27 10:19:05 2018 +0200
+++ b/dmenu-mpc/CMakeLists.txt	Fri Apr 27 10:20:02 2018 +0200
@@ -17,14 +17,14 @@
 
 project(dmenu-mpc)
 
-find_package(PkgConfig)
-
 pkg_check_modules(libmpdclient IMPORTED_TARGET libmpdclient)
 
 if (NOT libmpdclient_FOUND)
     set(WITH_MPC_MSG "No (libmpdclient not found)" CACHE "" INTERNAL FORCE)
 else ()
-    set(WITH_MPC_MSG "Yes" CACHE "" INTERNAL FORCE)
-    add_executable(dmenu-mpc main.cpp)
-    target_link_libraries(dmenu-mpc libdmenutools PkgConfig::libmpdclient)
+    define_executable(
+        NAME mpc
+        SOURCES main.cpp
+        LIBRARIES PkgConfig::libmpdclient
+    )
 endif ()