# HG changeset patch
# Parent 656d49c6f011212578c0a29e49c4d1566b8ad16e
maintainers: make UseRathaxes.cmake usable in standalone projects

- UseRathaxes now does more Rathaxes related things (like search for
  Codeworker);
- 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
@@ -27,27 +27,17 @@
 
 SET(CMAKE_MODULE_PATH ${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)
diff --git a/maintainers/CMakeLists.txt b/maintainers/CMakeLists.txt
--- a/maintainers/CMakeLists.txt
+++ b/maintainers/CMakeLists.txt
@@ -10,6 +10,6 @@
 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)
diff --git a/maintainers/CMakeScripts/FindCodeWorker.cmake b/maintainers/CMakeScripts/FindCodeWorker.cmake
--- a/maintainers/CMakeScripts/FindCodeWorker.cmake
+++ b/maintainers/CMakeScripts/FindCodeWorker.cmake
@@ -18,7 +18,11 @@
                  NAMES
                  codeworker
                  PATHS
-                 $ENV{CODEWORKER_DIR})
+                 $ENV{CODEWORKER_DIR}
+                 $ENV{RATHAXES_PATH}/lib/
+                 ${CMAKE_SYSTEM_LIBRARY_PATH}
+                 PATH_SUFFIXES
+                 rathaxes)
 
     # Else try to download it:
     IF (NOT CODEWORKER_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,5 @@
+# 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})
@@ -188,7 +190,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"
+        CONFIGURE_FILE("${CMAKE_CURRENT_LIST_DIR}/Templates/MakefileLKM.in"
                        "${MODULE_MAKEFILE}")
 
         SET(KERNEL_OBJECT_NAME "${RATHAXES_SOURCE}_${SYSTEM}.ko")
@@ -216,11 +218,11 @@
         # 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"
+        CONFIGURE_FILE("${CMAKE_CURRENT_LIST_DIR}/Templates/sources.in"
                        "${MODULE_SOURCES}")
 
        SET(WINDOWS_MAKEFILE
-           "${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts/Templates/Windows_Makefile")
+           "${CMAKE_CURRENT_LIST_DIR}/Templates/Windows_Makefile")
 
         SET(KERNEL_OBJECT_NAME "${RATHAXES_SOURCE}_${SYSTEM}.sys")
         ADD_CUSTOM_COMMAND(OUTPUT "${KERNEL_OBJECT_NAME}"
@@ -237,3 +239,24 @@
 	MESSAGE(STATUS "Don't know how to build kernel modules for ${SYSTEM} (yet)")
 ENDIF (${SYSTEM} MATCHES "Linux")
 ENDFUNCTION(ADD_RATHAXES_LKM NAME RATHAXES_SOURCE)
+
+# Code #########################################################################
+
+# Find additional dependencies:
+
+FIND_PACKAGE(CodeWorker REQUIRED)
+
+# Only do this on Linux, as far as I know nobody compiles Linux from another OS.
+IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
+    FIND_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/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% %*
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,6 +2,11 @@
 
 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 LOCAL_CACHE_PATH="%LOCALAPPDATA%\\Rathaxes\\cache\\"
diff --git a/rathaxes/CMakeLists.txt b/rathaxes/CMakeLists.txt
--- a/rathaxes/CMakeLists.txt
+++ b/rathaxes/CMakeLists.txt
@@ -26,4 +26,21 @@
 
 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)
+# CMakeScripts
+INSTALL(FILES
+        ${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts/UseRathaxes.cmake
+        ${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts/FindCodeWorker.cmake
+        ${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts/FindLinuxKBuildDir.cmake
+        DESTINATION share/doc/rathaxes/samples/CMakeScripts/)
+# Templates used by the CMakeScripts
+INSTALL(DIRECTORY ${RATHAXES_SOURCE_DIR}/maintainers/CMakeScripts/Templates
+        DESTINATION share/doc/rathaxes/samples/CMakeScripts/Template/)
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,12 @@
+PROJECT(RTX_SAMPLES C)
+
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+INCLUDE(CMakeScripts/UseRathaxes)
+
+ENABLE_TESTING()
+
+ADD_SUBDIRECTORY(helloworld)
+ADD_SUBDIRECTORY(syntax)
+ADD_SUBDIRECTORY(e1000)
+ADD_SUBDIRECTORY(windows_minimal_lkm)
