# How do i hook the LinkExecutable task in Native Builds

**URL:** <https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511>\
**Category:** Help/Discuss\
**Tags:** not-a-bug\
**Created:** [November 18, 2016, 2:30pm UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511 "2016-11-18T14:30:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ImGanesh](https://avatars.discourse-cdn.com/v4/letter/i/e95f7d/32.png) [@ImGanesh](https://discuss.gradle.org/u/ImGanesh)\
**Post date:** [November 18, 2016, 2:30pm UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/1 "2016-11-18T14:30:14Z")

</div>

Here is a set-up of my multi-project build

> project  
> |\_build.gradle  
> |\_subprojA  
> |\_\_build.gradle  
> |\_\_build  
> |\_\_\_exe  
> |\_\_\_\_subprojA  
> |\_\_\_\_\_subprojA  
> |\_subprojB  
> |\_\_build.gradle  
> |\_\_build  
> |\_\_\_exe  
> |\_\_\_\_subprojB  
> |\_\_\_\_\_subprojB

For linking objects created in different sub-directories, I would like to create a new custom task on the root project by using the LinkExecutable task type provided by Native Gradle. Attached is my incomplete build.gradle containing this custom task.

```
apply plugin: 'c'
model {

task myLinker(type: LinkExecutable) {
	  
       dependsOn << ":subprojA:subprojA_componentExecutable"
       dependsOn << ":subprojB:subprojB_componentExecutable"

		source {
                        subprojA/build/exe/subprojA/subprojA
                        subprojB/build/exe/subprojB/subprojB
                 }
       outputFile = file("build/exe/project")
  }

```

}

However, I get an error. I would like to link these object files and get a partially linked Executable. Any tips on the usage of this task type would be nice.

---

<div class="post-metadata">

**Author:** ![Daniel\_L](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daniel_l/32/6420_2.png) [@Daniel\_L](https://discuss.gradle.org/u/Daniel_L)\
**Post date:** [December 2, 2016, 9:47pm UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/2 "2016-12-02T21:47:57Z")

</div>

My bad for not responding to this question sooner. Although the `LinkExecutable` task is part of the public API, it isn’t expected for a user to create his own instance of that task. There are several factors to take into consideration. The public API of this task is the type for filtering (`tasks.withType(LinkExecutable)`).

We had other discussions regarding partial linking. If this is still a valid question, we can try to come up with a way to use `LinkExecutable` as a custom type. As a quick help, [this is how](https://github.com/gradle/gradle/blob/432c507f954c86db2e8c5866740baced63cfa482/subprojects/platform-native/src/main/java/org/gradle/nativeplatform/internal/NativeComponents.java#L54-L74) the `LinkExecutable` task is created in Gradle.

That been said, it is still a very valid use case to create your own `LinkExecutable`. You could [open a feature request](https://github.com/gradle/gradle/issues) to document this in the user guide as well as making sure you can achieve your specific use case without using internal API.

I how this helps,

Daniel

---

<div class="post-metadata">

**Author:** ![ImGanesh](https://avatars.discourse-cdn.com/v4/letter/i/e95f7d/32.png) [@ImGanesh](https://discuss.gradle.org/u/ImGanesh)\
**Post date:** [December 3, 2016, 7:34am UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/3 "2016-12-03T07:34:32Z")

</div>

Hi Daniel,

Thank you so much for the reply. I did manage to find a way of using the public API of the LinkExecutable task for my build system. It was a bit tricky to do it because I thought the Gradle documentation was not very comprehensive for this and will open a request to enhance it. I also went through your other answer on partial linking. And yes that I what I intend to do - extend the Native Model for building Partially Linked objects. However, I do have one nagging concern about the design.

Currently, for my root project’s `build.gradle`, I have the components part structured as follows:

```
components {
	CSWtooldb (NativeExecutableSpec) {
		targetPlatform "mySparc"
	}	
	CSWswdb (NativeExecutableSpec) {
		targetPlatform "mySparc"
	}
	tasks.withType (LinkExecutable) {
		if (name == "linkCSWtooldbExecutable") {

// the two tasks below copy their executables to ../objs_exe/tooldb

			dependsOn << ":root_proj/sub_projA:libsub_projA_tooldbExecutable"
			dependsOn << ":root_proj/sub_projB:libsub_projB_tooldbExecutable"
			source {
				fileTree (dir: 'root_proj/objs_exe/tooldb')
			}
		}		
		else if (name == "linkCSWswdbExecutable") {

// the two tasks below copy their executables to ../objs_exe/swdb

			dependsOn << ":root_proj/sub_projA:libsub_projA_swdbExecutable"
			dependsOn << ":root_proj/sub_projB:libsub_projB_swdbExecutable"	
			source {
				fileTree (dir: 'root_proj/objs_exe/swdb')
			}			
		}
	}
}

```

The idea is to build the root project using the partially linked sub-project objects. There are two components for the root project as well as for the sub-projects - they have the **same source set** but have different `api linkages`. (one is linked using header files from toolDB and other from swDB). Next, there is a second phase of link on the root project side. Clearly, the source for the linker in this case are the partially linked sub-projects’s objects. I want to also check if the sources are up to date before I do this second part of linking. If the sources have changed, then I want Gradle to first build and link the sub-project before doing the second phase of linking. So, I added the `dependsOn` part to the task.

1. I would like to know if this is the right way to do it given Gradle’s existing features. Or are there other ways in which Gradle helps with this type of builds? (for example - two components having the same source set but different linkages)
2. I have an `if` and `else if` closure in my task usage because I have no idea how to customize tasks component wise. For example, when building the tooldb variant of the root project, I want only the sub-project’s tooldb objects as source to the linker.
3. Is it possible to encapsulate these activities when I extend the Native model?
4. I think this is a bit far fetched question but anyways, I would also like to know if similar use cases have come up with other teams which use Gradle and if there is some documentation on how it was done.

Thanks a lot again!  
Ganesh

---

<div class="post-metadata">

**Author:** ![Daniel\_L](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daniel_l/32/6420_2.png) [@Daniel\_L](https://discuss.gradle.org/u/Daniel_L)\
**Post date:** [December 9, 2016, 10:06pm UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/4 "2016-12-09T22:06:14Z")

</div>

Hi Ganesh,

Always nice to answer your question as they are full of learning opportunities.

> 1. I would like to know if this is the right way to do it given Gradle’s existing features. Or are there other ways in which Gradle helps with this type of builds? (for example - two components having the same source set but different linkages)

Given my comprehension to what you are trying to do, your solution seems legit in solving your current use case. I have [opened an issue](https://github.com/gradle/gradle/issues/997) regarding better dependency management for native. You could have a look at it and add your comment/use case if you feel we are missing some information. A better dependency management would really help those kinds of scenario.

> 1. I have an `if` and `else if` closure in my task usage because I have no idea how to customize tasks component wise. For example, when building the tooldb variant of the root project, I want only the sub-project’s tooldb objects as source to the linker.

Thankfully, each `BinarySpec` has a [`getTasks()`](https://github.com/gradle/gradle/blob/master/subprojects/platform-base/src/main/java/org/gradle/platform/base/BinarySpec.java#L53-L56) method that returns all the task affecting the current binary. With this container (it’s a [`NamedDomainObjectSet`](https://docs.gradle.org/current/javadoc/org/gradle/api/NamedDomainObjectSet.html)) you can use `withType` as well as `matching`. However, it is assumed that only one `LinkExecutable` exists (may not always be true).

> 1. Is it possible to encapsulate these activities when I extend the Native model?

It is very easy to encapsulate those activities. I suggest creating rules in a plugin using [`RuleSource`](https://github.com/gradle/gradle/blob/master/subprojects/model-core/src/main/java/org/gradle/model/RuleSource.java) as it’s a bit more powerful than Groovy rules (using `$.`) and you can write them in Java to take advantage of static typing and Intellij code completion. For example, you could have a rule applied to every `NativeComponentSpec` like this:

```
class Rules extends RuleSource {
    @Mutate
    void applyToNativeComponent(@Each NativeComponentSpec component) {
        // do something
        component.binaries.withType(NativeExecutableBinarySpec) {
            tasks.withType(LinkExecutable) {
                // do something with this binary's link executable task
            }
        }
    }
}

```

> 1. I think this is a bit far fetched question but anyways, I would also like to know if similar use cases have come up with other teams which use Gradle and if there is some documentation on how it was done.

No question is far fetched. Native is a complex sphere as most build tools requires the user to specify _how_ they want the job to be done. Gradle paradigm is aligned with _what_ you want to do. For your particular case of partial linking, I don’t think this was explored with Gradle. Don’t hesitate to [open feature request](https://github.com/gradle/gradle/issues) for the various scenario that isn’t trivial to solve with Gradle.

### Bonus

The following code:  
`dependsOn << ":root_proj/sub_projA:libsub_projA_swdbExecutable"`  
is preferred to be written as:  
`dependsOn ":root_proj/sub_projA:libsub_projA_swdbExecutable"`  
The reason is you are invoking the getter for dependsOn property and appending to the list. This assumes the returned list from getDependsOn is the internal reference for the list. This may not always be true. In some case (elseware in the codebase), it’s a copy of the list that is returned and changing the returned list won’t have any effect as it’s going to get garbage collected right after.

I hope this helps,

Daniel

---

<div class="post-metadata">

**Author:** ![ImGanesh](https://avatars.discourse-cdn.com/v4/letter/i/e95f7d/32.png) [@ImGanesh](https://discuss.gradle.org/u/ImGanesh)\
**Post date:** [January 19, 2017, 9:59am UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/5 "2017-01-19T09:59:39Z")

</div>

> [@Daniel\_L](#):
>
> It is very easy to encapsulate those activities. I suggest creating rules in a plugin using RuleSource as it’s a bit more powerful than Groovy rules (using $.) and you can write them in Java to take advantage of static typing and Intellij code completion. For example, you could have a rule applied to every NativeComponentSpec like this:
> 
> class Rules extends RuleSource {  
> @Mutate  
> void applyToNativeComponent(@Each NativeComponentSpec component) {  
> // do something  
> component.binaries.withType(NativeExecutableBinarySpec) {  
> tasks.withType(LinkExecutable) {  
> // do something with this binary’s link executable task  
> }  
> }  
> }  
> }

Thank you. This was so helpful for me. I have used it for my activities. However, I am unable to use some substitutions like `$buildDir` and `${baseName}` as part of my rules. Like for example below

> class PartialLinkRules extends RuleSource {  
> @Mutate  
> void applyToNativeComponent (@Each NativeComponentSpec component) {  
> component.binaries.withType(NativeExecutableBinarySpec) {  
> tasks.withType(LinkExecutable) {  
> doFirst {  
> outputFile = file(“$buildDir/exe/${baseName}/${baseName}.o”)  
> }

I get the error such as

> Could not get unknown property ‘baseName’ for task ‘:my\_path:linkLibmysourceExecutable’ of type org.gradle.nativeplatform.tasks.LinkExecutable. How do I bring these variables within scope of my rules?

---

<div class="post-metadata">

**Author:** ![Daniel\_L](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daniel_l/32/6420_2.png) [@Daniel\_L](https://discuss.gradle.org/u/Daniel_L)\
**Post date:** [January 19, 2017, 1:15pm UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/6 "2017-01-19T13:15:11Z")

</div>

Thanks for coming back on this. For `buildDir`, you have to take a `File` input property and specify the model path with `@Path` annotation. As for `baseName`, it should pick it up according to the delegation rule of Groovy. However, in case it doesn’t pick it up, you can name the implicit argument for the closure passed to `withType(NativeExecutableBinarySpec)` and access the property via that named variable. For example,

```gradle
class PartialLinkRules extends RuleSource {
  @Mutate
  void applyToNativeComponent (@Each NativeComponentSpec component, @Path("buildDir") File buildDir) {
    component.binaries.withType(NativeExecutableBinarySpec) { binary ->
      tasks.withType(LinkExecutable) {
        doFirst {
          outputFile = file("$buildDir/exe/${binary.baseName}/${binary.baseName}.o")
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![ImGanesh](https://avatars.discourse-cdn.com/v4/letter/i/e95f7d/32.png) [@ImGanesh](https://discuss.gradle.org/u/ImGanesh)\
**Post date:** [January 20, 2017, 8:18am UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/7 "2017-01-20T08:18:29Z")

</div>

Thank you. Unfortunately the delegation rules or the implicit arguments passed to the function is not recognized by Gradle.

```
apply plugin: 'c'

class PartialLinkRules extends RuleSource {
	@Mutate
	void applyToNativeComponent (@Each NativeComponentSpec component, @Path("buildDir") File buildDir) {
		component.binaries.withType(NativeExecutableBinarySpec) { binary ->
			tasks.withType(LinkExecutable) {
				doFirst {
					outputFile = file("$buildDir/exe/${binary.baseName}/${binary.baseName}.o")
				}
			}
		}
	}
}

apply plugin: PartialLinkRules

```

> No such property: baseName for class: org.gradle.nativeplatform.NativeExecutableBinarySpec

Does it make a difference that this project is actually a sub-project in a multi-project configuration and this Rule definition is within the sub-project? The idea is to factor this out and make it apply to all sub-projects in the Multiconfig Project structure.

---

<div class="post-metadata">

**Author:** ![Daniel\_L](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daniel_l/32/6420_2.png) [@Daniel\_L](https://discuss.gradle.org/u/Daniel_L)\
**Post date:** [January 20, 2017, 12:05pm UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/8 "2017-01-20T12:05:24Z")

</div>

My bad, the [`baseName` is on the component](https://github.com/gradle/gradle/blob/master/subprojects/platform-native/src/main/java/org/gradle/nativeplatform/NativeComponentSpec.java#L26-L29). So the code should look like this:

```gradle
class PartialLinkRules extends RuleSource {
  @Mutate
  void applyToNativeComponent (@Each NativeComponentSpec component, @Path("buildDir") File buildDir) {
    component.binaries.withType(NativeExecutableBinarySpec) {
      tasks.withType(LinkExecutable) {
        doFirst {
          outputFile = file("$buildDir/exe/${component.baseName}/${component.baseName}.o")
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![ImGanesh](https://avatars.discourse-cdn.com/v4/letter/i/e95f7d/32.png) [@ImGanesh](https://discuss.gradle.org/u/ImGanesh)\
**Post date:** [January 20, 2017, 12:52pm UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/9 "2017-01-20T12:52:57Z")

</div>

Thanks. But unfortunately I get another error.

> Could not find method file() for arguments [/to/my/dir/build/exe/my\_component/my\_component.o] on task ‘:path\_to\_component:linkLibComponentExecutable’ of type org.gradle.nativeplatform.tasks.LinkExecutable.

---

<div class="post-metadata">

**Author:** ![Daniel\_L](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daniel_l/32/6420_2.png) [@Daniel\_L](https://discuss.gradle.org/u/Daniel_L)\
**Post date:** [January 20, 2017, 2:46pm UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/10 "2017-01-20T14:46:25Z")

</div>

The method [`file(...)` is part of the `Project` class](https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/java/org/gradle/api/Project.java#L611-L638). Since you default delegation of a build script is a `Project` instance, calling `file(...)` in the scope of the build script will work. The suggested workaround would be to use normal Java API like this:

```gradle
outputFile = new File(buildDir, "exe/${component.baseName}/${component.baseName}.

```

---

<div class="post-metadata">

**Author:** ![ImGanesh](https://avatars.discourse-cdn.com/v4/letter/i/e95f7d/32.png) [@ImGanesh](https://discuss.gradle.org/u/ImGanesh)\
**Post date:** [February 10, 2017, 9:22am UTC](https://discuss.gradle.org/t/how-do-i-hook-the-linkexecutable-task-in-native-builds/20511/11 "2017-02-10T09:22:30Z")

</div>

Thanks. I have one more question regarding this. Can I access the outputFile property of this task at configuration time? When I removed the doFirst closure, it did not work.
