123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. cmake_minimum_required( VERSION 2.8 )
  2. project( "slop" )
  3. set( slop_VERSION_MAJOR 4 )
  4. set( slop_VERSION_MINOR 1 )
  5. set( slop_VERSION_PATCH 15 )
  6. set( BIN_TARGET "${PROJECT_NAME}" )
  7. if( NOT CMAKE_INSTALL_PREFIX )
  8. set( CMAKE_INSTALL_PREFIX "/usr" )
  9. endif()
  10. if( NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE )
  11. set( CMAKE_BUILD_TYPE RelWithDebInfo )
  12. endif()
  13. # Linux compiler initialization.
  14. if ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR
  15. "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
  16. "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" )
  17. set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-parameter" )
  18. set( CMAKE_CXX_FLAGS_DEBUG "-Wextra -pedantic-errors -O0 -g" )
  19. set( CMAKE_CXX_FLAGS_RELEASE "-O2" )
  20. set( CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g" )
  21. # -Wall: Enable all warnings.
  22. # -Wextra: Enable some more warnings.
  23. # -Werror: Have errors on warnings.
  24. # -pedantic-errors: Even more errors.
  25. # -Wno-unused-parameter: Prevent unused variable warning. (Several functions are required to have unecessary variables because X11.)
  26. else()
  27. message( FATAL_ERROR "Your operating system isn't supported yet! CMake will now exit." )
  28. endif()
  29. # Add a check target for our makefile.
  30. find_program( CPPCHECK_EXECUTABLE cppcheck
  31. DOC "A tool for static C/C++ code analysis." )
  32. if (CPPCHECK_EXECUTABLE)
  33. add_custom_target( "check"
  34. COMMAND "${CPPCHECK_EXECUTABLE}" "--enable=all" "*"
  35. WORKING_DIRECTORY src VERBATIM )
  36. endif()
  37. # Here we generate some of our code if we can. I package it pre-generated
  38. # so nobody has to go find and install gengetopt if they don't want to.
  39. find_program( GENGETOPT_EXECUTABLE gengetopt
  40. DOC "A tool to generate code to grab command line options." )
  41. find_program( SED_EXECUTABLE sed )
  42. if ( GENGETOPT_EXECUTABLE AND SED_EXECUTABLE )
  43. message( "-- Regenerating cmdline.in" )
  44. # gengetopt generates cmdline.h, then we move it to cmdline.in.
  45. execute_process( COMMAND "${GENGETOPT_EXECUTABLE}" "--input=options.ggo"
  46. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src" )
  47. file( RENAME "${CMAKE_CURRENT_SOURCE_DIR}/src/cmdline.h" "${CMAKE_CURRENT_SOURCE_DIR}/src/cmdline.in" )
  48. # Due to a bug in gengetopt, we have to manually insert some code.
  49. # Replace the first instance of REPLACEME with some text.
  50. # Eight backslashes = two in the code because of how many instances of escaping is happening.
  51. execute_process( COMMAND "${SED_EXECUTABLE}" "-i" "0,/REPLACEME/{s/REPLACEME/X=%x\\\\\\\\nY=%y\\\\\\\\nW=%w\\\\\\\\nH=%h\\\\\\\\nG=%g\\\\\\\\nID=%i\\\\\\\\nCancel=%c\\\\\\\\n/}" "cmdline.c"
  52. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src" )
  53. # Then replace remaining instances.
  54. execute_process( COMMAND "${SED_EXECUTABLE}" "-i" "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. else()
  57. message( "Warning: Command gengetopt or sed not found! Won't regenerate command line code. (If you're just compiling this doesn't matter.)" )
  58. endif()
  59. # By default our src/options.ggo has our cmake versions variables for
  60. # the 'version ""' line. We replace them here.
  61. # The ${CMAKE_SOURCE_DIR} is there to fix problems with OpenBSD's out-of-source build black magic.
  62. configure_file( "src/cmdline.in" "${CMAKE_SOURCE_DIR}/src/cmdline.h" )
  63. # This allows for "make README.md" to be ran to update the README's help
  64. # section automatically. We don't add it to ALL because running arbitrary
  65. # scripts is unsafe and I don't know if systems will actually have it
  66. # be executbable.
  67. add_custom_target( README.md "./generateReadme.sh" DEPENDS "slop" )
  68. # Sources
  69. set( source
  70. src/cmdline.c
  71. src/rectangle.cpp
  72. src/x.cpp
  73. src/main.cpp )
  74. # Obtain library paths and make sure they exist.
  75. set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmakemodules" )
  76. find_package( X11 REQUIRED )
  77. find_package( XExt REQUIRED )
  78. # This library is needed only for Ubuntu it seems, some platforms don't even
  79. # ship with it. I couldn't find a way to do a test compile to check if librt
  80. # was needed, so instead I just didn't mark it as REQUIRED.
  81. find_package( RT )
  82. set( CMAKE_CXX_FLAGS
  83. "${CMAKE_CXX_FLAGS} ${CMAKE_IMLIB2_CXX_FLAGS}" )
  84. # Includes
  85. if ( RT_INCLUDE_DIR )
  86. include_directories( ${X11_INCLUDE_DIR}
  87. ${XEXT_INCLUDE_DIR}
  88. ${RT_INCLUDE_DIR} )
  89. else()
  90. include_directories( ${X11_INCLUDE_DIR}
  91. ${XEXT_INCLUDE_DIR} )
  92. endif()
  93. # Executable
  94. add_executable( "${BIN_TARGET}" ${source} )
  95. # Libraries
  96. if ( RT_LIBRARY )
  97. target_link_libraries( "${BIN_TARGET}"
  98. ${X11_LIBRARIES}
  99. "${XEXT_LIBRARY}"
  100. "${RT_LIBRARY}" )
  101. else()
  102. target_link_libraries( "${BIN_TARGET}"
  103. ${X11_LIBRARIES}
  104. "${XEXT_LIBRARY}" )
  105. endif()
  106. install( TARGETS ${BIN_TARGET}
  107. DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" )