On Wednesday 22 February 2012 17:14:06 Endi Sukma Dewata wrote:
 Andreas, 
Hi Endi,
 Here is a summary of our conversation. The PKI project consists of
 several components (e.g. util, common, ca, kra) and each might have
 source codes and test codes. We want to use CMake to generate the
 following Makefile targets:
 
   * all: This is the default target which should build only the main
 source code, not the test code.
 
   * test: This target will build and run the test code. It will use 2
 subtargets: build-test and run-test.
 
 They need to be separate targets because we might need to fix either the
 source or the test and then rerun the test without having to rebuild
 everything. 
This is possible but normally not mean to be done this way. The UseJava.cmake 
you're using in pki is outdated. IIRC java files get rebuilt always cause 
cmake detection if they are changed doesn't work. The lastest UseJava.cmake 
you find in cmake 2.8.7 detects changed files and only recompiles the changed 
files and targets depending on the target you changed.
 
 Currently this is what we have (see
 
http://fedorapeople.org/gitweb?p=edewata/public_git/pki.git;a=tree;f=pki;h=0
 b4261a0c77a41cf7ef5fc5af6ac3b9e73496720;hb=HEAD). The main CMakeLists.txt
 defines the main 'test' target:
 
    add_custom_target(test)
 
 This target is empty, but each component can attach itself to the main
 'test' target. For example the util package contains the following
 script (see base/util/test/CMakeLists.txt):
 
    ...
 
    # build util test
    add_jar(pki-util-test ...)
 
    ...
 
    # define util test target
    add_junit_test(test-pki-util ...)
 
    # attach util test to the main test target
    add_dependencies(test test-pki-util)
 
 The problem is the add_jar() is defined at the top level, so the test
 code will be built when we call 'make all', which is not what we want. 
I understand. That's how cmake works and is designed. If you want it in a 
diffrent way you need to do it with custom targets or you enable testing code 
with and option.
If the problem is the build time, use the UseJava.cmake which is upstream, it 
is much better.
 Is there a way to move add_jar() into a function/target such that
the
 test code will be built only when the 'test' target is called? 
I don't think so. You need to duplicate it and hack somthing which depends on 
your special target.
 
 So the main CMakeLists.txt will be something like this:
 
    # define the test target
    add_custom_target(test) 
cmake has a special target test used by ctest I think you should avoid the 
name.
option(WITH_TESTING "Turn on unit testing" OFF)
if (WITH_TESTING)
    # define the build-test subtarget
    add_custom_target(build-test)
    add_dependencies(test build-test)
 
    # define the run-test subtarget
    add_custom_target(run-test)
    add_dependencies(test run-test)
 
 and the util's CMakeLists.txt will be like this:
 
    # define a function/target to build util test
    function(build-pki-util-test ...)
      ...
      add_jar(pki-util-test ...)
      ...
    endfunction(build-pki-util-test ...)
 
    # attach the function/target to build-test
    add_dependencies(build-test build-pki-util-test)
 
 
    # define a function/target to run util test
    add_junit_test(run-pki-util-test ...)
 
    # attach the function/target to run-test
    add_dependencies(run-test run-pki-util-test) 
endif()
 
 If this is not possible, what would be the best alternative? Thanks! 
It is doable but what's wrong with building the tests all the time? Is it such 
a big problem? You could turn it off or on with an option.
 PS: We also talked about compiling Java codes without having to
specify
 the files explicitly, but that's for a separate discussion. 
UseJava.cmake is far from being complete and I'm not a Java developer. I'm 
adding features users of cmake send me and push them upstream. I've 
implemented the most common functions which were needed for PKI.
Cheers,
	-- andreas
-- 
Andreas Schneider                   GPG-ID: 8B7EB4B8
Red Hat                               asn(a)redhat.com
Samba Team                             asn(a)samba.org