# HG changeset patch
# Parent 237e60d20f4fd8063a48ff47cf37a8072f55478c
maintainers: add a new CMake "library" UseRathaxes.cmake to add a minimal Rathaxes support into CMake

- Add the new ADD_RATHAXES_EXECUTABLE function to build a .rtx target;
- No more generated files in the source directory;
- No more tedious generate shell script to write;
- ADD_RATHAXES_TEST is renamed to ADD_RATHAXES_CWS_TEST.


diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -33,7 +33,7 @@
 INCLUDE(AddFileDependencies)
 INCLUDE(FileCopy)
 INCLUDE(RathaxesCopyLatexClasses)
-INCLUDE(AddRathaxesTest)
+INCLUDE(UseRathaxes)
 
 # Global definitions ###########################################################
 
diff --git a/maintainers/CMakeScripts/AddRathaxesTest.cmake b/maintainers/CMakeScripts/AddRathaxesTest.cmake
deleted file mode 100644
--- a/maintainers/CMakeScripts/AddRathaxesTest.cmake
+++ /dev/null
@@ -1,8 +0,0 @@
-# This function call codeworker + cnorm with the given codeworker script.
-#
-# The codeworker script must return 0 on success.
-
-FUNCTION(ADD_RATHAXES_TEST CW_SCRIPT)
-    STRING(REGEX MATCH "(.*)\\.cws$" TEST_NAME "${CW_SCRIPT}")
-    ADD_TEST("Rathaxes_${TEST_NAME}" ${CODEWORKER_BINARY_PATH} "-I" "${RATHAXES_SOURCE_DIR}/maintainers/cnorm/src" "-I" "${RATHAXES_SOURCE_DIR}/rathaxes/compiler/" "-script" "${CMAKE_CURRENT_SOURCE_DIR}/${CW_SCRIPT}" "-args" "${RATHAXES_SOURCE_DIR}/rathaxes")
-ENDFUNCTION(ADD_RATHAXES_TEST CW_SCRIPT)
diff --git a/maintainers/CMakeScripts/UseRathaxes.cmake b/maintainers/CMakeScripts/UseRathaxes.cmake
new file mode 100644
--- /dev/null
+++ b/maintainers/CMakeScripts/UseRathaxes.cmake
@@ -0,0 +1,119 @@
+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})
+
+    FOREACH(I ${ARGS})
+        LIST(FIND OPTIONS "${I}" OPTION_IDX)
+        IF (NOT ${OPTION_IDX} EQUAL -1)
+            LIST(GET FILE_LISTS ${OPTION_IDX} CURRENT_FILE_LIST)
+        ELSE (NOT ${OPTION_IDX} EQUAL -1)
+            IF (DEFINED CURRENT_FILE_LIST)
+                LIST(APPEND ${CURRENT_FILE_LIST} "${I}")
+            ELSE (DEFINED CURRENT_FILE_LIST)
+                MESSAGE(SEND_ERROR "Unexpected argument: ${I}")
+            ENDIF (DEFINED CURRENT_FILE_LIST)
+        ENDIF (NOT ${OPTION_IDX} EQUAL -1)
+    ENDFOREACH(I ${ARGS})
+
+    IF (NOT DEFINED ${SYSTEM_OUT})
+        SET(${SYSTEM_OUT} ${CMAKE_SYSTEM_NAME})
+    ENDIF (NOT DEFINED ${SYSTEM_OUT})
+
+    # It's a "return value" variable so we have to "export" it with
+    # PARENT_SCOPE, otherwise it will stay local to the function.
+    #
+    # {RTI,BLT,SYSTEM}_OUT point to a variable name hence the double
+    # expand to get the value.
+    SET(${RTI_OUT} ${${RTI_OUT}} PARENT_SCOPE)
+    SET(${BLT_OUT} ${${BLT_OUT}} PARENT_SCOPE)
+    SET(${SYSTEM_OUT} ${${SYSTEM_OUT}} PARENT_SCOPE)
+ENDFUNCTION(_RTX_PARSE_OPTIONS ARGS RTI_OUT BLT_OUT)
+
+FUNCTION(_RTX_GENERATE_BUILD_COMMANDS REGISTER_COMMANDS_OUT RTI_FILES BLT_FILES GENERATE_COMMANDS_OUT SYSTEMS)
+    FOREACH(RTI ${RTI_FILES})
+        LIST(APPEND ${REGISTER_COMMANDS_OUT}
+             "COMMAND" ${_RTX_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}
+            "blt-register" "${CMAKE_CURRENT_SOURCE_DIR}/${BLT}")
+    ENDFOREACH(BLT ${BLT_FILES})
+
+    FOREACH(OS ${SYSTEMS})
+        LIST(APPEND ${GENERATE_COMMANDS_OUT}
+             COMMAND ${_RTX_CODEWORKER_COMMAND}
+             "generate" "${OS}"
+             "${CMAKE_CURRENT_SOURCE_DIR}/${RTX_FILE}")
+    ENDFOREACH(OS ${SYSTEMS})
+
+    SET(${REGISTER_COMMANDS_OUT} ${${REGISTER_COMMANDS_OUT}} PARENT_SCOPE)
+    SET(${GENERATE_COMMANDS_OUT} ${${GENERATE_COMMANDS_OUT}} PARENT_SCOPE)
+ENDFUNCTION(_RTX_GENERATE_BUILD_COMMANDS REGISTER_COMMANDS_OUT RTI_FILES BLT_FILES GENERATE_COMMANDS_OUT SYSTEMS)
+
+# This function will build a Rathaxes target. Usage:
+#
+# ADD_RATHAXES_EXECUTABLE(hello.rtx
+#                         [RTI hello.rti [file.rti]]
+#                         [BLT hello.blt [file.blt]]
+#                         [SYSTEM windows [linux]])
+#
+# Files are built in the same order as they are listed, except for the rtx file
+# which is built last.
+#
+# If no system are specified with SYSTEM then the source will be generated for
+# the current system (as in CMAKE_SYSTEM_NAME).
+#
+# At some point we will certainly have our own language definition for CMake
+# but let's start with simpler things.
+FUNCTION(ADD_RATHAXES_EXECUTABLE 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
+    # argument (otherwise it would expand as different arguments).
+    _RTX_PARSE_OPTIONS("${ARGV}" RTI_FILES BLT_FILES SYSTEMS)
+
+    _RTX_GENERATE_BUILD_COMMANDS(REGISTER_COMMANDS
+                                 "${RTI_FILES}"
+                                 "${BLT_FILES}"
+                                 GENERATE_COMMANDS
+                                 "${SYSTEMS}")
+
+    # As soon as we start to write the generation result to files this should
+    # be translated to an ADD_CUSTOM_COMMAND:
+    ADD_CUSTOM_TARGET(${OUT_NAME}
+                      ${_RTX_CODEWORKER_COMMAND} "cache" "clear"
+                      ${REGISTER_COMMANDS}
+                      COMMENT "Registering .rti and .blt for the Rathaxes target ${OUT_NAME}"
+                      VERBATIM
+                      SOURCES ${RTX_FILE} ${RTI_FILES} ${BLT_FILES})
+
+    STRING(REPLACE ";" ", " SYSTEMS "${SYSTEMS}")
+    ADD_CUSTOM_COMMAND(TARGET ${OUT_NAME} POST_BUILD
+                       ${GENERATE_COMMANDS}
+                       COMMENT "Building Rathaxes target ${OUT_NAME} for ${SYSTEMS}"
+                       VERBATIM)
+ENDFUNCTION(ADD_RATHAXES_EXECUTABLE OUT_NAME RTX_FILE)
+
+# This function call codeworker + cnorm with the given codeworker script. The
+# path to the rathaxes source directory is given as the first argument (arg[0]).
+#
+# The codeworker script must return 0 on success.
+FUNCTION(ADD_RATHAXES_CWS_TEST CW_SCRIPT)
+    STRING(REGEX MATCH "(.*)\\.cws$" TEST_NAME "${CW_SCRIPT}")
+    ADD_TEST("Rathaxes_${TEST_NAME}"
+             ${CODEWORKER_BINARY_PATH}
+             "-I" "${RATHAXES_SOURCE_DIR}/maintainers/cnorm/src"
+             "-I" "${RATHAXES_SOURCE_DIR}/rathaxes/compiler/"
+             "-script" "${CMAKE_CURRENT_SOURCE_DIR}/${CW_SCRIPT}"
+             "-args" "${RATHAXES_SOURCE_DIR}/rathaxes")
+ENDFUNCTION(ADD_RATHAXES_CWS_TEST CW_SCRIPT)
diff --git a/rathaxes/compiler/tests/CMakeLists.txt b/rathaxes/compiler/tests/CMakeLists.txt
--- a/rathaxes/compiler/tests/CMakeLists.txt
+++ b/rathaxes/compiler/tests/CMakeLists.txt
@@ -1,39 +1,39 @@
 # Pure backend compilation and resolution tests
-ADD_RATHAXES_TEST(01_A_test_back_parse.cws)
-ADD_RATHAXES_TEST(01_B_test_back_compile.cws)
-ADD_RATHAXES_TEST(01_C_test_back_meta.cws)
-ADD_RATHAXES_TEST(01_D_test_back_type_hash.cws)
-ADD_RATHAXES_TEST(01_E_test_back_introspect.cws)
-ADD_RATHAXES_TEST(01_F_test_back_gen.cws)
-ADD_RATHAXES_TEST(01_G_test_back_registerToCache.cws)
-ADD_RATHAXES_TEST(01_H_test_back_loadFromCache.cws)
-ADD_RATHAXES_TEST(01_I_test_back_resolveCall.cws)
-ADD_RATHAXES_TEST(01_J_test_back_resolvePointcut.cws)
-ADD_RATHAXES_TEST(01_K_test_back_resolvePointcutDefault.cws)
-ADD_RATHAXES_TEST(01_L_test_back_resolveTypeMapping.cws)
-ADD_RATHAXES_TEST(01_M_test_back_resolveTypeChunkCall.cws)
-ADD_RATHAXES_TEST(01_N_test_back_resolveSequenceCall.cws)
+ADD_RATHAXES_CWS_TEST(01_A_test_back_parse.cws)
+ADD_RATHAXES_CWS_TEST(01_B_test_back_compile.cws)
+ADD_RATHAXES_CWS_TEST(01_C_test_back_meta.cws)
+ADD_RATHAXES_CWS_TEST(01_D_test_back_type_hash.cws)
+ADD_RATHAXES_CWS_TEST(01_E_test_back_introspect.cws)
+ADD_RATHAXES_CWS_TEST(01_F_test_back_gen.cws)
+ADD_RATHAXES_CWS_TEST(01_G_test_back_registerToCache.cws)
+ADD_RATHAXES_CWS_TEST(01_H_test_back_loadFromCache.cws)
+ADD_RATHAXES_CWS_TEST(01_I_test_back_resolveCall.cws)
+ADD_RATHAXES_CWS_TEST(01_J_test_back_resolvePointcut.cws)
+ADD_RATHAXES_CWS_TEST(01_K_test_back_resolvePointcutDefault.cws)
+ADD_RATHAXES_CWS_TEST(01_L_test_back_resolveTypeMapping.cws)
+ADD_RATHAXES_CWS_TEST(01_M_test_back_resolveTypeChunkCall.cws)
+ADD_RATHAXES_CWS_TEST(01_N_test_back_resolveSequenceCall.cws)
 
 # Pure middle-end validation tests
-ADD_RATHAXES_TEST(02_A_test_middle_parse.cws)
-ADD_RATHAXES_TEST(02_B_test_middle_parse_rti_file.cws)
-ADD_RATHAXES_TEST(02_C_test_middle_check_inheritance.cws)
-ADD_RATHAXES_TEST(02_D_test_middle_check_types_declaration.cws)
+ADD_RATHAXES_CWS_TEST(02_A_test_middle_parse.cws)
+ADD_RATHAXES_CWS_TEST(02_B_test_middle_parse_rti_file.cws)
+ADD_RATHAXES_CWS_TEST(02_C_test_middle_check_inheritance.cws)
+ADD_RATHAXES_CWS_TEST(02_D_test_middle_check_types_declaration.cws)
 
 # Required for test 2_B
 FILE_COPY(full_lkm/simplified_lkm.rti full_lkm/simplified_lkm.rti)
 
 # Tests for configuration matching (against with blocks)
-ADD_RATHAXES_TEST(03_A_test_config_match_lesser.cws)
-ADD_RATHAXES_TEST(03_B_test_config_match_lesser_or_equal.cws)
-ADD_RATHAXES_TEST(03_C_test_config_match_equal.cws)
-ADD_RATHAXES_TEST(03_D_test_config_match_greater_or_equal.cws)
-ADD_RATHAXES_TEST(03_E_test_config_match_greater.cws)
-ADD_RATHAXES_TEST(03_F_test_config_match.cws)
+ADD_RATHAXES_CWS_TEST(03_A_test_config_match_lesser.cws)
+ADD_RATHAXES_CWS_TEST(03_B_test_config_match_lesser_or_equal.cws)
+ADD_RATHAXES_CWS_TEST(03_C_test_config_match_equal.cws)
+ADD_RATHAXES_CWS_TEST(03_D_test_config_match_greater_or_equal.cws)
+ADD_RATHAXES_CWS_TEST(03_E_test_config_match_greater.cws)
+ADD_RATHAXES_CWS_TEST(03_F_test_config_match.cws)
 
 # Tests for the Front-end
-ADD_RATHAXES_TEST(04_A_test_front_parse.cws)
+ADD_RATHAXES_CWS_TEST(04_A_test_front_parse.cws)
 
 # Tests for Link module
-ADD_RATHAXES_TEST(05_A_test_link_merge.cws)
-ADD_RATHAXES_TEST(05_B_test_link_merge_conflict.cws)
+ADD_RATHAXES_CWS_TEST(05_A_test_link_merge.cws)
+ADD_RATHAXES_CWS_TEST(05_B_test_link_merge_conflict.cws)
