How to add all files in a fileset as an argument to the exec task?
Frage
I'm trying to provide all *.cpp files in a folder to the c++ compiler through ant. But I get no further than ant giving gpp a giant string containing all the files. I tried to prove it by using a small test application:
int main( int argc, char**args ){
for( --argc; argc != 0; --argc ) printf("arg[%d]: %s\n",argc,args[argc]);
}
With the ant script like this:
<target name="cmdline">
<fileset id="fileset" dir=".">
<include name="*"/>
</fileset>
<pathconvert refid="fileset" property="converted"/>
<exec executable="a.exe">
<arg value="${converted}"/>
</exec>
</target>
My a.exe's output is this:
[exec] arg[1]: .a.cpp.swp .build.xml.swp a.cpp a.exe build.xml
Now here's the question: how do I provide all files in the fileset individually as an argument to the executable?
Lösung
This is what the apply task in ANT was designed to support.
For example:
<target name="cmdline">
<apply executable="a.exe" parallel="true">
<srcfile/>
<fileset dir="." includes="*.cpp"/>
</apply>
</target>
The parallel argument runs the program once using all the files as arguments.
Andere Tipps
Found it: the difference seems to lie in arg value
vs. arg line
.
<arg line="${converted}"/>
resulted in the expected output:
[exec] arg[5]: C:\cygwin\home\xtofl_2\antes\build.xml
[exec] arg[4]: C:\cygwin\home\xtofl_2\antes\a.exe
[exec] arg[3]: C:\cygwin\home\xtofl_2\antes\a.cpp
[exec] arg[2]: C:\cygwin\home\xtofl_2\antes\.build.xml.swp
[exec] arg[1]: C:\cygwin\home\xtofl_2\antes\.a.cpp.swp
Based on this article, here is the complete code illustrating the use of the pathconvert
task:
<target name="atask">
<fileset dir="dir" id="myTxts">
<include name="*.txt" />
</fileset>
<pathconvert property="cmdTxts" refid="myTxts" pathsep=" " />
<apply executable="${cmd}" parallel="false" verbose="true">
<arg value="-in" />
<srcfile />
<arg line="${cmdTxts}" />
<fileset dir="${list.dir}" includes="*.list" />
</apply>
</target>
Above code assumes there are no spaces in the paths.
To support spaces in paths, change above pathconvert
line to:
<pathconvert property="cmdTxts" refid="myTxts" pathsep="' '" />
and arg
line to:
<arg line="'${cmdTxts}'"/>
Have you looked at the ant cpptasks
? This would allow you to integrate C++ compilation into your Ant build in a more Ant-centric fashion. For example, specifying files to be compiled using a fileset.
Here is the example (compatible with Ant 1.6 or later)::
<project name="hello" default="compile" xmlns:cpptasks="antlib:net.sf.antcontrib.cpptasks">
<target name="compile">
<mkdir dir="target/main/obj"/>
<cpptasks:cc outtype="executable" subsystem="console" outfile="target/hello" objdir="target/main/obj">
<fileset dir="src/main/c" includes="*.c"/>
</cpptasks:cc>
</target>
</project>