[native] copy files into all install directories


(Ralf Schallenberg) #1

Hi,

how do I create a copy task to copy certain files and folders from known locations to all of an executable’s install directories? Each executable has four binaries: x64Debug, x64Release, x86Debug, and x64Release. I need to copy a couple of additional files and folder into all of the install directories that are created for those four binaries. I can’t figure out how to do that.
Is a copy task the right solution or can I define some kind of file/folder dependency for each binary?

Thanks!


(Ralf Schallenberg) #2

Ok, this is what I got so far. It works, but it isn’t pretty. Is there a more concise way to achieve the same? With less redundancy and less hard-coding?

def myRuntimeFiles() {
    return fileTree(dir: 'src/MyExecutableComponent/Release', includes: ['some_file.dat', 'Data/**'])
}

task MyExecutableComponentCopy1(type: Copy) {
    from myRuntimeFiles()
    into 'build/install/myExecutableComponentExecutable/x64Debug/lib'
}
task MyExecutableComponentCopy2(type: Copy) {
    from myRuntimeFiles()
    into 'build/install/myExecutableComponentExecutable/x64Release/lib'
}
task MyExecutableComponentCopy3(type: Copy) {
    from myRuntimeFiles()
    into 'build/install/myExecutableComponentExecutable/x86Debug/lib'
}
task MyExecutableComponentCopy4(type: Copy) {
    from myRuntimeFiles()
    into 'build/install/myExecutableComponentExecutable/x86Release/lib'
}

task MyExecutableComponentCopy << {
    println "MyExecutableComponentCopy tasks finished."
}
MyExecutableComponentCopy.dependsOn {
    [MyExecutableComponentCopy1, MyExecutableComponentCopy2, MyExecutableComponentCopy3, MyExecutableComponentCopy4]
}

install.dependsOn {
    [MyExecutableComponentCopy]
}

(Ralf Schallenberg) #3

Why doesn’t this work? Would have been really nice and easy.

MyExecutableComponent(NativeExecutableSpec) {
    // [...]
    dependencies {
        runtime fileTree(dir: 'src/MyExecutableComponent/Release', includes: ['some_file.dat', 'Data/**'])
    }
    // [...]
}

(Tony Abbott) #4

Hi Ralf,

I think this will do what you want. It adds an action to the end of the install task for each binary to copy your runtime files. It also adds those files as inputs to the task so if you change those files the up-to-date check will fail and cause the task to run again.

binaries.withType(NativeExecutableBinarySpec) { bin ->
    bin.tasks.withType(InstallExecutable) { installTask ->
        def runtimeFiles = myRuntimeFiles()
        installTask.inputs.files runtimeFiles
        installTask << {
            fileOperations.copy(new Action<CopySpec>() {
                void execute(CopySpec copySpec) {
                    copySpec.into(installTask.destinationDir)
                    copySpec.from(runtimeFiles)
                }   
            })  
        }   
    }   
}

Is it possible to have a native software build task copy a header file into the directory where the binaries are placed?
(Ralf Schallenberg) #5

Thanks a lot! This does work nicely. I’ll try to put that into a function to use it in different executable components as the runtime files may differ between them.


(Adrian Lis) #6

Sorry for bringing this back up but I am struggling with the same issue and found this solution. I am trying to do pretty much the same thing yet the solution provided doesn’t work for me.

when I use installTask << { print “Foo” } nothing happens also I guess fileOperations is something legacy that now does not work or am I missing something? I tried to put a copy { } inside but does that not work either.

When I try to convert this to a task and use installTask.dependsOn << task { } I get error that I try to mutate the closed view.

How can I achieve this? I really need to be able to copy some files to the install directories/output and have access to currently build targetPlatform and buildType which I have access to using this approach (the bin and install).


(Tony Abbott) #7

Is the build failing or are you just not seeing “Foo” in the output? If it’s just not outputting then it sounds like the action isn’t being invoked. Make sure you are invoking the install task - it’s not called automatically by build. I haven’t tried this with the latest gradle but it appears fileOperations is still there. I’d get the print working first before trying to copy files.

If you still don’t come right then post your build.gradle that shows exactly what you’re trying to do.