Browse Source

Improved CMakeLists.txt

naelstrof 9 years ago
parent
commit
c8cf1ad28a
6 changed files with 65 additions and 17 deletions
  1. 2
    0
      .gitignore
  2. 60
    8
      CMakeLists.txt
  3. 0
    6
      gengetopt.sh
  4. 1
    1
      src/cmdline.c
  5. 1
    1
      src/cmdline.in
  6. 1
    1
      src/options.ggo

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+# These files are ignored since cmake generates them from cmdline.in
2
+src/cmdline.h

+ 60
- 8
CMakeLists.txt View File

@@ -1,20 +1,23 @@
1 1
 cmake_minimum_required( VERSION 2.8 )
2 2
 
3
-set( PROJECT_NAME   "slop" )
3
+project( "slop" )
4
+set( slop_VERSION_MAJOR 3 )
5
+set( slop_VERSION_MINOR 1 )
6
+set( slop_VERSION_PATCH 8 )
7
+
4 8
 set( BIN_TARGET     "${PROJECT_NAME}" )
5 9
 set( CMAKE_INSTALL_PREFIX "/usr/bin" )
6 10
 
7
-project( ${PROJECT_NAME} )
8
-
9 11
 # Linux compiler initialization.
10
-if ( CMAKE_COMPILER_IS_GNUCXX )
12
+if ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR
13
+     "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
14
+     "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" )
11 15
     set( CMAKE_CXX_FLAGS
12 16
          "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -pedantic-errors -Wno-unused-parameter" )
13 17
          # -Wall:   Enable all warnings.
14 18
          # -Wextra: Enable some more warnings.
15 19
          # -Werror: Have errors on warnings.
16 20
          # -Wno-unused-parameter: Prevent unused variable warning. (Several functions are required to have unecessary variables because X11.)
17
-    add_definitions( -D_CMAKE_LINUX_ )
18 21
 else()
19 22
     message( FATAL_ERROR "Your operating system isn't supported yet! CMake will now exit." )
20 23
 endif()
@@ -28,6 +31,55 @@ if (CPPCHECK_EXECUTABLE)
28 31
                        WORKING_DIRECTORY src VERBATIM )
29 32
 endif()
30 33
 
34
+# Here we generate some of our code if we can. I package it pre-generated
35
+# so nobody has to go find and install gengetopt if they don't want to.
36
+find_program( GENGETOPT_EXECUTABLE gengetopt
37
+              DOC "A tool to generate code to grab command line options." )
38
+find_program( SED_EXECUTABLE sed
39
+              DOC "A text replacement tool used to help generate code" )
40
+find_program( COPY_EXECUTABLE cp )
41
+if ( GENGETOPT_EXECUTABLE AND SED_EXECUTABLE AND COPY_EXECUTABLE )
42
+    message( "Executing " "${GENGETOPT_EXECUTABLE}" " " "--input=options.ggo" " " "--header-extension=in" )
43
+    execute_process( COMMAND
44
+                     "${GENGETOPT_EXECUTABLE}" "--input=options.ggo"
45
+                     WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src" )
46
+    execute_process( COMMAND
47
+                     "${COPY_EXECUTABLE}" "cmdline.h" "cmdline.in"
48
+                     WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src" )
49
+    # Due to a bug in gengetopt, we have to manually insert some code.
50
+    # Replace the first instance of REPLACEME with some text.
51
+    # Eight backslashes = two in the code because of how many instances of escaping is happening.
52
+    message( "Replacing REPLACEME in generated code..." )
53
+    execute_process( COMMAND
54
+                     "${SED_EXECUTABLE}" "-i" "0,/REPLACEME/{s/REPLACEME/X=%x\\\\\\\\nY=%y\\\\\\\\nW=%w\\\\\\\\nH=%h\\\\\\\\nG=%g\\\\\\\\nID=%i\\\\\\\\nCancel=%c\\\\\\\\n/}" "cmdline.c"
55
+                     WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src" )
56
+    # Then replace remaining instances.
57
+    execute_process( COMMAND
58
+                     "${SED_EXECUTABLE}" "-i" "s/REPLACEME/X=%x\\\\nY=%y\\\\nW=%w\\\\nH=%h\\\\nG=%g\\\\nID=%i\\\\nCancel=%c\\\\n/" "cmdline.c"
59
+                     WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src" )
60
+else()
61
+    message( "Command gengetopt or sed not found! Won't regenerate command line code." )
62
+endif()
63
+
64
+# By default our src/options.ggo has our cmake versions variables for
65
+# the 'version ""' line. We replace them here.
66
+configure_file( "src/cmdline.in"
67
+                "src/cmdline.h" )
68
+
69
+# Here we need to make sure our README has the correct version inside of it.
70
+# We use a simple regex search and replace for it, but it seems really
71
+# hard because cmake is silly.
72
+# I think this is better than using configure_file since README.md needs
73
+# to exist within github, rather than as an input file with a different
74
+# name or extension.
75
+message( "Replacing version in readme..." )
76
+set( SEARCH_REGEX "slop v([0-9]+)\\.([0-9]+)\\.([0-9]+)" )
77
+set( REPLACE_TEXT "slop v${slop_VERSION_MAJOR}.${slop_VERSION_MINOR}.${slop_VERSION_PATCH}" )
78
+file( READ "${CMAKE_CURRENT_SOURCE_DIR}/README.md" FILE_CONTENT )
79
+string( REGEX REPLACE "${SEARCH_REGEX}" "${REPLACE_TEXT}"
80
+        MODIFIED_FILE_CONTENT "${FILE_CONTENT}" )
81
+file( WRITE "${CMAKE_CURRENT_SOURCE_DIR}/README.md" "${MODIFIED_FILE_CONTENT}" )
82
+
31 83
 # Sources
32 84
 set( source
33 85
      src/cmdline.c
@@ -48,12 +100,12 @@ include_directories( ${X11_INCLUDE_DIR}
48 100
                      ${XEXT_INCLUDE_DIR} )
49 101
 
50 102
 # Executable
51
-add_executable( ${BIN_TARGET} ${source} )
103
+add_executable( "${BIN_TARGET}" ${source} )
52 104
 
53 105
 # Libraries
54
-target_link_libraries( ${BIN_TARGET}
106
+target_link_libraries( "${BIN_TARGET}"
55 107
                        ${X11_LIBRARIES}
56
-                       ${XEXT_LIBRARY} )
108
+                       "${XEXT_LIBRARY}" )
57 109
 
58 110
 install( TARGETS ${BIN_TARGET}
59 111
          DESTINATION  ${CMAKE_INSTALL_PREFIX} )

+ 0
- 6
gengetopt.sh View File

@@ -1,6 +0,0 @@
1
-#!/bin/bash
2
-# This is used because a bug in gengetopt keeps you from putting \n in default string options. All it does is replace REPLACEME with the correct code to make the application work properly.
3
-cd "$(dirname $0)/src"
4
-gengetopt < options.ggo
5
-sed -i '0,/REPLACEME/{s/REPLACEME/X=%x\\\\nY=%y\\\\nW=%w\\\\nH=%h\\\\nG=%g\\\\nID=%i\\\\nCancel=%c\\\\n/}' cmdline.c
6
-sed -i 's/REPLACEME/X=%x\\nY=%y\\nW=%w\\nH=%h\\nG=%g\\nID=%i\\nCancel=%c\\n/' cmdline.c

+ 1
- 1
src/cmdline.c View File

@@ -1,7 +1,7 @@
1 1
 /*
2 2
   File autogenerated by gengetopt version 2.22.6
3 3
   generated with the following command:
4
-  gengetopt 
4
+  /usr/bin/gengetopt --input=options.ggo 
5 5
 
6 6
   The developers of gengetopt consider the fixed text that goes in all
7 7
   gengetopt output files to be in the public domain:

src/cmdline.h → src/cmdline.in View File

@@ -31,7 +31,7 @@ extern "C" {
31 31
 
32 32
 #ifndef CMDLINE_PARSER_VERSION
33 33
 /** @brief the program version */
34
-#define CMDLINE_PARSER_VERSION "v3.1.8"
34
+#define CMDLINE_PARSER_VERSION "v@slop_VERSION_MAJOR@.@slop_VERSION_MINOR@.@slop_VERSION_PATCH@"
35 35
 #endif
36 36
 
37 37
 /** @brief Where the command line options are stored */

+ 1
- 1
src/options.ggo View File

@@ -1,5 +1,5 @@
1 1
 package "slop"
2
-version "v3.1.8"
2
+version "v@slop_VERSION_MAJOR@.@slop_VERSION_MINOR@.@slop_VERSION_PATCH@"
3 3
 usage "slop [options]"
4 4
 description "slop (Select Operation) is an application that queries for a selection from the user and prints the region to stdout."
5 5
 versiontext "Copyright (C) 2014 Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors)"