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