How to add all files in a fileset as an argument to the exec task?
Asked Answered
C

4

12

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?

Cyndi answered 13/11, 2011 at 18:24 Comment(0)
G
14

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.

Griswold answered 15/11, 2011 at 18:52 Comment(0)
C
5

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
Cyndi answered 13/11, 2011 at 18:35 Comment(2)
small confusion.. did you forget to change <arg value> to <arg line> in your own answer ?Showing
@PulakAgrawal: yes... Too excited :)Cyndi
P
0

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>
Pastry answered 14/11, 2011 at 10:25 Comment(2)
I have; either it's in rigor mortis, or it's dead. (i.e. it doesn't seem to be maintained anymore). Do you have experience with it?Cyndi
I worked on a mainly-Java project where it was used to build C code using gcc for a few modules. It is still in use there. Note that it is ant-contrib as a whole (rather than cpptasks in particular) which hasn't seen a new release since 2008. If I was reviewing ant-contrib now having not used it before, I guess I would share your concern. However, I think ant-contrib is still widely used.Pastry
J
0

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}'"/>

Source: Converting an Ant fileset to multiple apply args.

Jaws answered 23/1, 2018 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.