# HG changeset patch
# Parent 0861918d54e44cb25af0153bcac15647351ef6b1
maintainers: make UseRathaxes.cmake usable in standalone projects

- UseRathaxes now does more Rathaxes related things (like search for
  Codeworker);
- UseRathaxes (as well as FindCodeWorker, FindRathaxes...) could be distributed
  with Rathaxes (i.e installed when you install Rathaxes) but then you have the
  problem of finding the CMakeLists files from your (separate) project. That's
  why I think it's simpler to just copy the CMakeLists each time in each
  project (for now);
- Currently used by the examples;
- Should make the first steps guide really usable now;

diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -25,29 +25,19 @@
     # FILE(REMOVE "CMakeCache.txt") doesn't work so include it into the snippet.
 ENDIF (${RATHAXES_SOURCE_DIR} STREQUAL ${RATHAXES_BINARY_DIR})
 
-SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts)
+LIST(APPEND CMAKE_MODULE_PATH ${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts)
 
+INCLUDE(UseRathaxes)
 FIND_PACKAGE(LATEX)
-FIND_PACKAGE(CodeWorker REQUIRED)
-FIND_PACKAGE(LinuxKBuildDir)
 
 INCLUDE(AddFileDependencies)
 INCLUDE(FileCopy)
 INCLUDE(RathaxesCopyLatexClasses)
-INCLUDE(UseRathaxes)
-
-IF (CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT LINUX_KBUILD_DIR)
-    MESSAGE(STATUS "No valid Linux build tree found, kernel modules will not be built")
-ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT LINUX_KBUILD_DIR)
 
 # Global definitions ###########################################################
 
 ENABLE_TESTING()
 
-IF (CMAKE_COMPILER_IS_GNUCC)
-    SET(CMAKE_C_FLAGS "-pipe -Wextra -Wall -std=c99")
-ENDIF (CMAKE_COMPILER_IS_GNUCC)
-
 # Subdirectories ###############################################################
 
 IF (PDFLATEX_COMPILER AND PS2PDF_CONVERTER)
@@ -87,7 +77,7 @@
 SET(PACKAGE_NAME "rathaxes")
 SET(IGNORE_PATTERNS
     "/\\\\.hg.*"
-    ".*\\\\.sw.?$"
+    ".*\\\\.sw.$"
     "/CMakeFiles/"
     "CMakeCache.txt"
     ".*install.*\\\\.(cmake|txt)$"
diff --git a/maintainers/CMakeLists.txt b/maintainers/CMakeLists.txt
--- a/maintainers/CMakeLists.txt
+++ b/maintainers/CMakeLists.txt
@@ -10,6 +10,10 @@
 INSTALL(PROGRAMS ${CODEWORKER_BINARY_PATH}
         DESTINATION lib/rathaxes/)
 
-# Generate the install rules for the rathaxes binaries:
+# Generate the install rules for the rathaxes binaries
 
 ADD_SUBDIRECTORY(bin)
+
+# Install the Rathaxes CMakeScripts library
+
+ADD_SUBDIRECTORY(CMakeScripts)
diff --git a/maintainers/CMakeScripts/CMakeLists.txt b/maintainers/CMakeScripts/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/maintainers/CMakeScripts/CMakeLists.txt
@@ -0,0 +1,9 @@
+# CMakeScripts
+INSTALL(FILES
+        FindCodeWorker.cmake
+        FindRathaxes.cmake
+        FindLinuxKBuildDir.cmake
+        DESTINATION share/rathaxes/CMakeScripts/)
+
+# Templates used by the CMakeScripts
+INSTALL(DIRECTORY Templates DESTINATION share/rathaxes/CMakeScripts/)
diff --git a/maintainers/CMakeScripts/FindCodeWorker.cmake b/maintainers/CMakeScripts/FindCodeWorker.cmake
--- a/maintainers/CMakeScripts/FindCodeWorker.cmake
+++ b/maintainers/CMakeScripts/FindCodeWorker.cmake
@@ -50,7 +50,7 @@
                 IF (UNIX)
                     EXECUTE_PROCESS(COMMAND "chmod" "+x" "${CODEWORKER_BINARY_PATH}")
                     IF (CMAKE_SYSTEM_NAME MATCHES "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
-                        MESSAGE(STATUS "CodeWorker needs the 32bit compatability libraries (ia32-libs), make sure you have them!")
+                        MESSAGE(STATUS "CodeWorker needs the 32bit compatibility libraries (ia32-libs), make sure you have them!")
                     ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
                 ENDIF (UNIX)
             ELSE (${CODEWORKER_DL_RETURN} EQUAL 0)
diff --git a/maintainers/CMakeScripts/FindLinuxKBuildDir.cmake b/maintainers/CMakeScripts/FindLinuxKBuildDir.cmake
--- a/maintainers/CMakeScripts/FindLinuxKBuildDir.cmake
+++ b/maintainers/CMakeScripts/FindLinuxKBuildDir.cmake
@@ -34,6 +34,8 @@
             SET(LINUX_KBUILD_DIR "${KDIR}" CACHE PATH "Path to the Linux KBuild directory (usually /lib/modules/`uname -r`/build/)")
         ENDIF (LINUX_KBUILD_DIR_MAKEFILE)
     ENDIF (IS_DIRECTORY "${KDIR}")
+ELSE (NOT LINUX_KBUILD_DIR)
+    SET(LINUXKBUILDDIR_FIND_QUIETLY TRUE)
 ENDIF (NOT LINUX_KBUILD_DIR)
 
 INCLUDE(FindPackageHandleStandardArgs)
diff --git a/maintainers/CMakeScripts/FindRathaxes.cmake b/maintainers/CMakeScripts/FindRathaxes.cmake
new file mode 100644
--- /dev/null
+++ b/maintainers/CMakeScripts/FindRathaxes.cmake
@@ -0,0 +1,19 @@
+# Package Finder for Rathaxes and CMake.
+#
+# This finder simply seek for the rathaxes compiler and stores its path in the
+# variable RATHAXES_BINARY_PATH, if rathaxes is found.
+#
+# This finder is used internally by UseRathaxes.
+
+IF (NOT RATHAXES_BINARY_PATH)
+    FIND_PROGRAM(RATHAXES_BINARY_PATH
+                 NAMES
+                 rathaxes
+                 rathaxes.bat)
+ELSE (NOT RATHAXES_BINARY_PATH)
+    SET(RATHAXES_FIND_QUIETLY TRUE)
+ENDIF (NOT RATHAXES_BINARY_PATH)
+
+INCLUDE(FindPackageHandleStandardArgs)
+
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(Rathaxes DEFAULT_MSG RATHAXES_BINARY_PATH)
diff --git a/maintainers/CMakeScripts/UseRathaxes.cmake b/maintainers/CMakeScripts/UseRathaxes.cmake
--- a/maintainers/CMakeScripts/UseRathaxes.cmake
+++ b/maintainers/CMakeScripts/UseRathaxes.cmake
@@ -1,3 +1,11 @@
+# CMake library to easily work on Rathaxes projects.
+#
+# Simply INCLUDE() it in your Rathaxes project. Obviously you need to have
+# Rathaxes installed to use it (unless you are a Rathaxes developer and
+# directly working on Rathaxes itself).
+
+# Functions ####################################################################
+
 FUNCTION(_RTX_PARSE_OPTIONS ARGS RTI_OUT BLT_OUT SYSTEM_OUT)
     SET(OPTIONS "RTI" "BLT" "SYSTEM")
     SET(FILE_LISTS ${RTI_OUT} ${BLT_OUT} ${SYSTEM_OUT})
@@ -38,13 +46,13 @@
          SYSTEMS)               # [in] name of each target system
     FOREACH(RTI ${RTI_FILES})
         LIST(APPEND ${REGISTER_COMMANDS_OUT}
-             "COMMAND" ${_RTX_CODEWORKER_COMMAND}
+             "COMMAND" ${_USE_RATHAXES_CODEWORKER_COMMAND}
              "rti-register" "${CMAKE_CURRENT_SOURCE_DIR}/${RTI}")
     ENDFOREACH(RTI ${RTI_FILES})
 
     FOREACH(BLT ${BLT_FILES})
         LIST(APPEND ${REGISTER_COMMANDS_OUT}
-            "COMMAND" ${_RTX_CODEWORKER_COMMAND}
+            "COMMAND" ${_USE_RATHAXES_CODEWORKER_COMMAND}
             "blt-register" "${CMAKE_CURRENT_SOURCE_DIR}/${BLT}")
     ENDFOREACH(BLT ${BLT_FILES})
 
@@ -53,7 +61,7 @@
         # At some point I guess we will use different directories instead of
         # different file names.
         LIST(APPEND ${GENERATE_COMMANDS_OUT}
-             COMMAND ${_RTX_CODEWORKER_COMMAND}
+             COMMAND ${_USE_RATHAXES_CODEWORKER_COMMAND}
              "generate" "/o" "${OUT_NAME}_${OS}.c" "${OS}"
              "${CMAKE_CURRENT_SOURCE_DIR}/${RTX_FILE}")
     ENDFOREACH(OS ${SYSTEMS})
@@ -85,13 +93,6 @@
 # At some point we will certainly have our own language definition for CMake
 # but let's start with simpler things.
 FUNCTION(ADD_RATHAXES_SOURCES OUT_NAME RTX_FILE)
-    SET(_RTX_CODEWORKER_COMMAND
-        ${CODEWORKER_BINARY_PATH} "-nologo"
-        "-I" "${RATHAXES_SOURCE_DIR}/maintainers/cnorm/src"
-        "-I" "${RATHAXES_SOURCE_DIR}/rathaxes/compiler/"
-        "-script" "${CMAKE_SOURCE_DIR}/rathaxes/compiler/rathaxes.cws"
-        "-args" "${CMAKE_SOURCE_DIR}/rathaxes/" "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/RathaxesCache/")
-
     LIST(REMOVE_ITEM ARGV ${OUT_NAME} ${RTX_FILE})
 
     # We have to expand ARGV inside quotes, so it expand as a single LIST
@@ -118,7 +119,7 @@
     # that should be rebuilt automatically when its sources change.
     STRING(REPLACE ";" ", " SYSTEMS "${SYSTEMS}")
     ADD_CUSTOM_COMMAND(OUTPUT ${OUTPUTS}
-                       COMMAND ${_RTX_CODEWORKER_COMMAND} "cache" "clear"
+                       COMMAND ${_USE_RATHAXES_CODEWORKER_COMMAND} "cache" "clear"
                        ${REGISTER_COMMANDS}
                        ${GENERATE_COMMANDS}
                        COMMENT "Building Rathaxes target ${OUT_NAME} for ${SYSTEMS}"
@@ -188,8 +189,7 @@
         # Generate the Linux Makefile to build a Linux kernel module
         SET(MODULE_MAKEFILE "${MODULE_BUILD_DIR}/Makefile")
         SET(LKM_OBJECTS "${RATHAXES_SOURCE}_${SYSTEM}.o")
-        CONFIGURE_FILE("${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts/Templates/MakefileLKM.in"
-                       "${MODULE_MAKEFILE}")
+        CONFIGURE_FILE("${_USE_RATHAXES_LIST_DIR}/Templates/MakefileLKM.in" "${MODULE_MAKEFILE}")
 
         SET(KERNEL_OBJECT_NAME "${RATHAXES_SOURCE}_${SYSTEM}.ko")
         ADD_CUSTOM_COMMAND(OUTPUT "${KERNEL_OBJECT_NAME}"
@@ -216,11 +216,9 @@
         # Generate the Windows Makefile to build a Windows kernel module
         SET(MODULE_SOURCES "${MODULE_BUILD_DIR}/sources")
         SET(LKM_SOURCES "${RATHAXES_SOURCE}_${SYSTEM}.c")
-        CONFIGURE_FILE("${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts/Templates/sources.in"
-                       "${MODULE_SOURCES}")
+        CONFIGURE_FILE("${_USE_RATHAXES_LIST_DIR}/Templates/sources.in" "${MODULE_SOURCES}")
 
-       SET(WINDOWS_MAKEFILE
-           "${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts/Templates/Windows_Makefile")
+       SET(WINDOWS_MAKEFILE "${_USE_RATHAXES_LIST_DIR}/Templates/Windows_Makefile")
 
         SET(KERNEL_OBJECT_NAME "${RATHAXES_SOURCE}_${SYSTEM}.sys")
         ADD_CUSTOM_COMMAND(OUTPUT "${KERNEL_OBJECT_NAME}"
@@ -237,3 +235,58 @@
 	MESSAGE(STATUS "Don't know how to build kernel modules for ${SYSTEM} (yet)")
 ENDIF (${SYSTEM} MATCHES "Linux")
 ENDFUNCTION(ADD_RATHAXES_LKM NAME RATHAXES_SOURCE)
+
+# Like FIND_PACKAGE but also search in the Rathaxes directories. Usage:
+#
+# FIND_RATHAXES_PACKAGE(PACKAGE [REQUIRED])
+FUNCTION(FIND_RATHAXES_PACKAGE NAME)
+    # The environment variable is set by rathaxesvars.bat on Windows
+    FOREACH(I ${CMAKE_SYSTEM_PREFIX_PATH} $ENV{RATHAXES_PATH})
+        LIST(APPEND CMAKE_MODULE_PATH "${I}/share/rathaxes/CMakeScripts")
+    ENDFOREACH(I ${CMAKE_SYSTEM_PREFIX_PATH} $ENV{RATHAXES_PATH})
+    FIND_PACKAGE(${NAME} ${ARGV2})
+ENDFUNCTION(FIND_RATHAXES_PACKAGE NAME)
+
+# Code #########################################################################
+
+# Inside a FUNCTION OR MACRO CMAKE_CURRENT_LIST_{DIR,FILE} return the path of
+# the caller CMakeLists.txt. Additionaly, CMAKE_CURRENT_LIST_DIR is a feature
+# of CMake >= 2.8.3.
+GET_FILENAME_COMPONENT(_USE_RATHAXES_LIST_DIR ${CMAKE_CURRENT_LIST_FILE} PATH CACHE)
+MARK_AS_ADVANCED(_USE_RATHAXES_LIST_DIR)
+
+# Find additional dependencies:
+
+# If we are within the Rathaxes project directly use CodeWorker and directly
+# point to the compiler files within the repository.
+# Else use the "system" rathaxes binary.
+IF (${PROJECT_NAME} STREQUAL RATHAXES)
+    FIND_RATHAXES_PACKAGE(CodeWorker REQUIRED)
+
+    SET(_USE_RATHAXES_CODEWORKER_COMMAND
+        ${CODEWORKER_BINARY_PATH} "-nologo"
+        "-I" "${RATHAXES_SOURCE_DIR}/maintainers/cnorm/src"
+        "-I" "${RATHAXES_SOURCE_DIR}/rathaxes/compiler/"
+        "-script" "${CMAKE_SOURCE_DIR}/rathaxes/compiler/rathaxes.cws"
+        "-args" "${CMAKE_SOURCE_DIR}/rathaxes/" "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/RathaxesCache/"
+        CACHE INTERNAL "CodeWorker command to -manually- invoke the Rathaxes compiler")
+ELSE (${PROJECT_NAME} STREQUAL RATHAXES)
+    FIND_RATHAXES_PACKAGE(Rathaxes REQUIRED)
+
+    SET(_USE_RATHAXES_CODEWORKER_COMMAND ${RATHAXES_BINARY_PATH} CACHE INTERNAL "Rathaxes command")
+ENDIF (${PROJECT_NAME} STREQUAL RATHAXES)
+
+# Only do this on Linux, as far as I know nobody compiles Linux from another OS.
+IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
+    FIND_RATHAXES_PACKAGE(LinuxKBuildDir)
+    IF (LINUX_KBUILD_DIR)
+        MESSAGE(STATUS "No valid Linux build tree found, Linux kernel modules will not be built")
+    ENDIF (LINUX_KBUILD_DIR)
+ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux")
+
+# Set some compiler options:
+
+# Set some sane flags that should be used in every project made with Rathaxes
+IF (CMAKE_COMPILER_IS_GNUCC)
+    SET(CMAKE_C_FLAGS "-pipe -Wextra -Wall -std=c99")
+ENDIF (CMAKE_COMPILER_IS_GNUCC)
diff --git a/maintainers/bin/CMakeLists.txt b/maintainers/bin/CMakeLists.txt
--- a/maintainers/bin/CMakeLists.txt
+++ b/maintainers/bin/CMakeLists.txt
@@ -19,7 +19,7 @@
 ENDIF (WIN32)
 
 SET(CNORM_PATH "${PATH_PREFIX}/lib/rathaxes/cnorm/")
-SET(COMPILER_PATH "${PATH_PREFIX}/lib/rathaxes/compiler/")
+SET(RATHAXES_LIB "${PATH_PREFIX}/lib/rathaxes")
 SET(CODEWORKER_PATH "${PATH_PREFIX}/lib/rathaxes/codeworker${CMAKE_EXECUTABLE_SUFFIX}")
 
 CONFIGURE_FILE("${RATHAXES_SCRIPT_NAME}.in" "${RATHAXES_SCRIPT_NAME}" @ONLY)
diff --git a/maintainers/bin/rathaxes.bat b/maintainers/bin/rathaxes.bat
--- a/maintainers/bin/rathaxes.bat
+++ b/maintainers/bin/rathaxes.bat
@@ -1,6 +1,3 @@
 @echo off
 
-rem We have to explicitely give the path of the script as the first argument on
-rem Windows.
-
-%CODEWORKER% --quiet %INCLUDE_PATHS% -script %COMPILER_PATH%/rathaxes.cws -args %COMPILER_PATH%/rathaxes.cws %LOCAL_CACHE_PATH% %*
+%CODEWORKER% --quiet %INCLUDE_PATHS% -script %COMPILER_PATH%/rathaxes.cws -args %RATHAXES_LIB% %LOCAL_CACHE_PATH% %*
diff --git a/maintainers/bin/rathaxes.in b/maintainers/bin/rathaxes.in
--- a/maintainers/bin/rathaxes.in
+++ b/maintainers/bin/rathaxes.in
@@ -1,10 +1,11 @@
 #!/bin/sh
 
 CNORM_PATH="@CNORM_PATH@"
-COMPILER_PATH="@COMPILER_PATH@"
+RATHAXES_LIB="@RATHAXES_LIB@"
+COMPILER_PATH="$RATHAXES_LIB/compiler"
 LOCAL_CACHE_PATH=${LOCAL_CACHE_PATH:-~/.rathaxes/cache}
 
 INCLUDE_PATHS="-I $CNORM_PATH -I $COMPILER_PATH"
 
 exec @CODEWORKER_PATH@ --quiet $INCLUDE_PATHS \
-         -script $COMPILER_PATH/rathaxes.cws -args $COMPILER_PATH $LOCAL_CACHE_PATH $*
+         -script $COMPILER_PATH/rathaxes.cws -args $RATHAXES_LIB $LOCAL_CACHE_PATH $*
diff --git a/maintainers/bin/rathaxesvars.bat.in b/maintainers/bin/rathaxesvars.bat.in
--- a/maintainers/bin/rathaxesvars.bat.in
+++ b/maintainers/bin/rathaxesvars.bat.in
@@ -2,8 +2,14 @@
 
 echo -- Setting up the Rathaxes environment
 
+rem This variable is simply used as a shortcut to the Rathaxes install
+rem directory (since we never know where it can be on Windows). Useful to
+rem specify some paths in the documentation. This is also used by the
+rem FindCodeWorker CMake finder.
+set RATHAXES_PATH=%CD%
 set CNORM_PATH="%CD%@CNORM_PATH@"
-set COMPILER_PATH="%CD%@COMPILER_PATH@"
+set RATHAXES_LIB="%CD%@RATHAXES_LIB@"
+set COMPILER_PATH="%CD%@RATHAXES_LIB@/compiler"
 set LOCAL_CACHE_PATH="%LOCALAPPDATA%\\Rathaxes\\cache\\"
 
 set INCLUDE_PATHS=-I %CNORM_PATH% -I %COMPILER_PATH%
diff --git a/rathaxes/CMakeLists.txt b/rathaxes/CMakeLists.txt
--- a/rathaxes/CMakeLists.txt
+++ b/rathaxes/CMakeLists.txt
@@ -26,4 +26,17 @@
 
 INSTALL(DIRECTORY compiler DESTINATION lib/rathaxes
         PATTERN compiler/tests EXCLUDE)
-INSTALL(DIRECTORY samples DESTINATION share/doc/rathaxes)
+INSTALL(DIRECTORY samples DESTINATION share/doc/rathaxes
+        PATTERN "SamplesCMakeLists.txt" EXCLUDE)
+
+# Install rules for the "standalone" build system to generate the examples:
+
+# Top-level CMakeLists.txt
+INSTALL(FILES samples/SamplesCMakeLists.txt
+        DESTINATION share/doc/rathaxes/samples
+        RENAME CMakeLists.txt)
+
+# UseRathaxes.cmake
+INSTALL(FILES
+        ${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts/UseRathaxes.cmake
+        DESTINATION share/doc/rathaxes/samples/CMakeScripts/)
diff --git a/rathaxes/samples/SamplesCMakeLists.txt b/rathaxes/samples/SamplesCMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/rathaxes/samples/SamplesCMakeLists.txt
@@ -0,0 +1,14 @@
+PROJECT(RTX_SAMPLES C)
+
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeScripts)
+
+INCLUDE(UseRathaxes)
+
+ENABLE_TESTING()
+
+ADD_SUBDIRECTORY(helloworld)
+ADD_SUBDIRECTORY(syntax)
+ADD_SUBDIRECTORY(e1000)
+ADD_SUBDIRECTORY(windows_minimal_lkm)
diff --git a/rathaxes/samples/windows_minimal_lkm/CMakeLists.txt b/rathaxes/samples/windows_minimal_lkm/CMakeLists.txt
--- a/rathaxes/samples/windows_minimal_lkm/CMakeLists.txt
+++ b/rathaxes/samples/windows_minimal_lkm/CMakeLists.txt
@@ -3,5 +3,9 @@
                      BLT log.blt lkm.blt)
 
 IF (WIN32)
-    ADD_RATHAXES_LKM(minimal_lkm minimal_lkm_src)
+    IF ("_not_a_var_$ENV{DDKBUILDENV}" STREQUAL "_not_a_var_chk")
+        ADD_RATHAXES_LKM(minimal_lkm minimal_lkm_src)
+    ELSE ("_not_a_var_$ENV{DDKBUILDENV}" STREQUAL "_not_a_var_chk")
+        MESSAGE(STATUS "Not in the Checked Windows Driver Development Kit environment, not building the Windows LKM")
+    ENDIF ("_not_a_var_$ENV{DDKBUILDENV}" STREQUAL "_not_a_var_chk")
 ENDIF (WIN32)
