From c36fcf2fd8c419dcae905f360916d95330f61121 Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Wed, 18 Jul 2012 13:13:34 -0500
Subject: [PATCH] Added support for exclusion in Java CMake script.

The Java CMake scripts has been modified to support excluding some
files from compilation or JAR packaging.

Ticket #62
---
 cmake/Modules/Java.cmake         |   28 ++++++++++++++++++----------
 cmake/Modules/JavaFileList.cmake |   22 ++++++++++++++++++++--
 2 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/cmake/Modules/Java.cmake b/cmake/Modules/Java.cmake
index bab17f54136d18b432393ead4187ac8d360e958f..62f26f9fcc9902bca5e6110bf5e1081c4de65aee 100644
--- a/cmake/Modules/Java.cmake
+++ b/cmake/Modules/Java.cmake
@@ -24,10 +24,10 @@ function(javac target)
 
     foreach (arg ${ARGN})
 
-        if (arg MATCHES "(SOURCE_DIR|SOURCES|CLASSPATH|OUTPUT_DIR|DEPENDS)")
+        if (arg MATCHES "(SOURCE_DIR|SOURCES|EXCLUDE|CLASSPATH|OUTPUT_DIR|DEPENDS)")
             set(param ${arg})
 
-        else (arg MATCHES "(SOURCE_DIR|SOURCES|CLASSPATH|OUTPUT_DIR|DEPENDS)")
+        else (arg MATCHES "(SOURCE_DIR|SOURCES|EXCLUDE|CLASSPATH|OUTPUT_DIR|DEPENDS)")
 
             if (param MATCHES "SOURCE_DIR")
                 set(source_dir ${arg})
@@ -35,6 +35,9 @@ function(javac target)
             elseif (param MATCHES "SOURCES")
                 list(APPEND sources ${arg})
 
+            elseif (param MATCHES "EXCLUDE")
+                list(APPEND exclude ${arg})
+
             elseif (param MATCHES "CLASSPATH")
                 list(APPEND classpath ${arg})
 
@@ -46,7 +49,7 @@ function(javac target)
 
             endif(param MATCHES "SOURCE_DIR")
 
-        endif(arg MATCHES "(SOURCE_DIR|SOURCES|CLASSPATH|OUTPUT_DIR|DEPENDS)")
+        endif(arg MATCHES "(SOURCE_DIR|SOURCES|EXCLUDE|CLASSPATH|OUTPUT_DIR|DEPENDS)")
 
     endforeach(arg)
 
@@ -60,22 +63,23 @@ function(javac target)
        set(native_classpath "${native_classpath}${separator}${path}")
     endforeach(path)
 
-    set(filelist "${CMAKE_CURRENT_BINARY_DIR}/${target}.files")
+    set(file_list "${CMAKE_CURRENT_BINARY_DIR}/${target}.files")
 
     add_custom_target(${target} ALL DEPENDS ${depends})
 
     add_custom_command(
         TARGET ${target}
         COMMAND ${CMAKE_COMMAND}
-            -Doutput=${filelist}
+            -Doutput=${file_list}
             -Dinput_dir=${source_dir}
             -Dfiles="${sources}"
+            -Dexclude="${exclude}"
             -P ${CMAKE_MODULE_PATH}/JavaFileList.cmake
         COMMAND ${CMAKE_Java_COMPILER}
             ${CMAKE_JAVA_COMPILE_FLAGS}
             -cp ${native_classpath}
             -d ${output_dir}
-            @${filelist}
+            @${file_list}
         WORKING_DIRECTORY
             ${source_dir}
     )
@@ -96,7 +100,7 @@ function(jar target)
             set(param ${arg})
             set(operation "u")
 
-        elseif (arg MATCHES "(INPUT_DIR|FILES|DEPENDS)")
+        elseif (arg MATCHES "(INPUT_DIR|FILES|EXCLUDE|DEPENDS)")
             set(param ${arg})
 
         else ()
@@ -110,6 +114,9 @@ function(jar target)
             elseif (param MATCHES "FILES")
                 list(APPEND files ${arg})
 
+            elseif (param MATCHES "EXCLUDE")
+                list(APPEND exclude ${arg})
+
             elseif (param MATCHES "DEPENDS")
                 list(APPEND depends ${arg})
 
@@ -119,20 +126,21 @@ function(jar target)
 
     endforeach(arg)
 
-    set(filelist "${CMAKE_CURRENT_BINARY_DIR}/${target}.files")
+    set(file_list "${CMAKE_CURRENT_BINARY_DIR}/${target}.files")
 
     add_custom_target(${target} ALL DEPENDS ${depends})
 
     add_custom_command(
         TARGET ${target}
         COMMAND ${CMAKE_COMMAND}
-            -Doutput=${filelist}
+            -Doutput=${file_list}
             -Dinput_dir=${input_dir}
             -Dfiles="${files}"
+            -Dexclude="${exclude}"
             -P ${CMAKE_MODULE_PATH}/JavaFileList.cmake
         COMMAND ${CMAKE_Java_ARCHIVE}
             -${operation}f ${output}
-            @${filelist}
+            -C ${input_dir} @${file_list}
         WORKING_DIRECTORY ${input_dir}
     )
 
diff --git a/cmake/Modules/JavaFileList.cmake b/cmake/Modules/JavaFileList.cmake
index 6ebdaea126e7af77b463b35fbec394a940d5faca..ef6f99f2dc9f9135ea3ba4bdfaf0d45ed0eb3cbc 100644
--- a/cmake/Modules/JavaFileList.cmake
+++ b/cmake/Modules/JavaFileList.cmake
@@ -21,6 +21,18 @@
 file(WRITE ${output} "")
 
 separate_arguments(file_list UNIX_COMMAND ${files})
+separate_arguments(exclude_list UNIX_COMMAND ${exclude})
+
+foreach(file ${exclude_list})
+
+    file(GLOB_RECURSE absolute_files "${input_dir}/${file}")
+
+    foreach(absolute_file ${absolute_files})
+        file(RELATIVE_PATH relative_file ${input_dir} ${absolute_file})
+	    list(APPEND excluded_files ${relative_file})
+    endforeach(absolute_file ${absolute_files})
+
+endforeach(file ${exclude_list})
 
 foreach(file ${file_list})
 
@@ -28,7 +40,13 @@ foreach(file ${file_list})
 
     foreach(absolute_file ${absolute_files})
         file(RELATIVE_PATH relative_file ${input_dir} ${absolute_file})
-	    file(APPEND ${output} "${relative_file}\n")
+
+        list(FIND excluded_files ${relative_file} index)
+
+        if (${index} EQUAL -1)
+	        file(APPEND ${output} "${relative_file}\n")
+	    endif(${index} EQUAL -1)
+
     endforeach(absolute_file ${absolute_files})
 
-endforeach(file ${files})
+endforeach(file ${file_list})
-- 
1.7.7.6

