Skip to content

Commit

Permalink
Added support for noExecArgs. Fixed unit test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ysb33r committed Apr 28, 2015
1 parent 79e80ad commit 4b0fed8
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 36 deletions.
57 changes: 31 additions & 26 deletions src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuild.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.ysb33r.gradle.gnumake.internal.Executor
import org.ysb33r.gradle.gnumake.internal.InputOutputMonitor
import org.ysb33r.gradle.gnumake.internal.MakeExecutor
import org.ysb33r.gradle.gnumake.internal.Rules
import org.ysb33r.gradle.gnumake.internal.TaskUtils

/**
* A wrapper task for calling GNU Make. This is useful for migrating legacy builds
Expand All @@ -56,38 +57,39 @@ class GnuMakeBuild extends DefaultTask {
* This is equivalent of passing -B to make
*/
@Input
@Optional
boolean alwaysMake = false

/** Tell make that varibales from the environment takes precedence over variables defined
* inside makefile.
* This is equivalent of passing -e to make
*/
@Input
@Optional
boolean environmentOverrides = false

/** Tell make to ignore all errors in commands executed to remake files.
* This is equivalant to passing -i to make
* This is equivalent to passing -i to make
*/
@Input
@Optional
boolean ignoreErrors = false

/** Set make concurrency level.
* This is equivalent of passing -j to make.
*/
@Input
@Optional
Integer jobs = 1

/** Tell make to carry on as far as possible after errors.
* This is equivalant to passing -k to make
*/
@Input
@Optional
boolean keepGoing = false

/** If set to {@code true} then {@code gnumake.execArgs} are not inherited.
* Default is to inherit.
*/
@Input
boolean noExecArgs = false


/** List of targets to execute
*
Expand Down Expand Up @@ -125,7 +127,7 @@ class GnuMakeBuild extends DefaultTask {
@Optional
@CompileDynamic
Map getFlags() {
if( defaultFlags && project.extensions.findByName('gnumake') ) {
if( defaultFlags && project.extensions.findByName(GnuMakeExtension.EXTENSION_NAME) ) {
project.gnumake.defaultFlags + this.flags
} else {
this.flags
Expand Down Expand Up @@ -188,7 +190,7 @@ class GnuMakeBuild extends DefaultTask {
@Input
@CompileDynamic
String getExecutable() {
this.executable ?: project.extensions.getByName('gnumake').executable
this.executable ?: project.extensions.getByName(GnuMakeExtension.EXTENSION_NAME)?.executable
}

/** The make executable.
Expand Down Expand Up @@ -258,11 +260,14 @@ class GnuMakeBuild extends DefaultTask {
/** Makefile to use. If not supplied will resort to the default behaviour of make,
* which is usually to look for a file called Makefile or GNUMakefile.
* This is the equivalent of passing -f to make.
*
* @return Returns the makefile or null
*/
@Input
@Optional
@CompileDynamic
String getMakefile() {
this.makefile ?: project.extensions.getByName('gnumake').makefile
this.makefile ?: project.extensions.findByName(GnuMakeExtension.EXTENSION_NAME)?.makefile
}

/** Makefile to use. If not supplied will resort to the default behaviour of make,
Expand Down Expand Up @@ -355,7 +360,7 @@ class GnuMakeBuild extends DefaultTask {
executor = new MakeExecutor(project)
}
buildCmdArgs()
execResult = executor.runMake(executable,cmdArgs,getWorkingDir())
execResult = executor.runMake(getExecutable(),cmdArgs,getWorkingDir())
}

@Deprecated
Expand Down Expand Up @@ -403,22 +408,22 @@ class GnuMakeBuild extends DefaultTask {
@CompileDynamic
@PackageScope
List<String> buildCmdArgs() {

cmdargs = project.extensions.getByName('gnumake').execArgs + [
[alwaysMake, '-B'],
[environmentOverrides, '-e'],
[ignoreErrors, '-i'],
[keepGoing, '-k'],
[jobs > 1, '-j', jobs as String],
[makefile, '-f', "${makefile.toString()}"],
[chDir, '-C', "${chDir.toString()}"],

].collectMany { it.head() ? it.tail() : [] } +

(getIncludeDirs().files.collectMany { ['-I', "${it.toString()}"] }) +
targets +
flags.collect { k, v -> "$k=$v" } +
switches
cmdargs = TaskUtils.buildCmdArgs(project,this,targets)
// cmdargs = project.extensions.getByName(GnuMakeExtension.EXTENSION_NAME).execArgs + [
// [alwaysMake, '-B'],
// [environmentOverrides, '-e'],
// [ignoreErrors, '-i'],
// [keepGoing, '-k'],
// [jobs > 1, '-j', jobs as String],
// [makefile, '-f', "${makefile.toString()}"],
// [chDir, '-C', "${chDir.toString()}"],
//
// ].collectMany { it.head() ? it.tail() : [] } +
//
// (getIncludeDirs().files.collectMany { ['-I', "${it.toString()}"] }) +
// targets +
// flags.collect { k, v -> "$k=$v" } +
// switches
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import org.gradle.util.CollectionUtils
*/
class GnuMakeExtension {

static final String EXTENSION_NAME = 'gnumake'

String executable = 'make'
String makefile

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.gradle.api.Project
class GnuMakePlugin implements Plugin<Project> {
void apply(Project project) {

project.extensions.create('gnumake', GnuMakeExtension, project)
project.extensions.create(GnuMakeExtension.EXTENSION_NAME, GnuMakeExtension, project)
project.tasks.create( 'make', GnuMakeBuild ).configure {
description "Runs a GNU Make process"
group "GNU Make tasks"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,40 @@
// ============================================================================
//

package org.ysb33r.gradle.gnumake.internal

import org.gradle.api.Project
import org.gradle.process.ExecResult
import org.ysb33r.gradle.gnumake.GnuMakeBuild
import org.ysb33r.gradle.gnumake.GnuMakeExtension

/**
* @author Schalk W. Cronjé
*/
class TaskUtils {
static List<String> buildCmdArgs(Project project,GnuMakeBuild task,List<String> targets) {

project.extensions.getByName('gnumake').execArgs + [
List<String> execArgs = []

if (!task.noExecArgs) {
execArgs = project.extensions.findByName(GnuMakeExtension.EXTENSION_NAME)?.execArgs ?: []
}

def switches = [
[task.alwaysMake, '-B'],
[task.environmentOverrides, '-e'],
[task.ignoreErrors, '-i'],
[task.keepGoing, '-k'],
[task.jobs > 1, '-j', task.jobs.toString()],
[task.makefile, '-f', "${task.makefile.toString()}"],
[task.chDir, '-C', "${task.chDir?.absolutePath}" ],
].collectMany { it.head() ? it.tail() : [] } +
].collectMany { it.head() ? it.tail() : [] }

def includes = (task.includeDirs.files.collectMany { ['-I', "${it.absolutePath}"] })

def flags = task.flags.collect { k, v -> "$k=$v" }

(task.includeDirs.files.collectMany { ['-I', "${it.absolutePath}"] }) +
targets +
task.flags.collect { k, v -> "$k=$v" } +
task.switches
execArgs + switches + includes + targets + flags + task.switches
}

static ExecResult runMake(Project project,final String exec,final List<String> cmdargs,final File wd=null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ============================================================================
//

import org.ysb33r.gradle.gnumake.internal.FakeExecutor
import spock.lang.*
import org.ysb33r.gradle.gnumake.GnuMakeBuild
import org.gradle.api.Project
Expand Down Expand Up @@ -160,7 +161,7 @@ class GnuMakeBuildSpec extends spock.lang.Specification {
expect:
gnumake.cmdArgs.size() == 2
gnumake.cmdArgs[0] == '-C'
gnumake.cmdArgs[1] == "./change/here"
gnumake.cmdArgs[1] == project.file('./change/here').absolutePath

}

Expand Down Expand Up @@ -300,6 +301,23 @@ class GnuMakeBuildSpec extends spock.lang.Specification {
gnumake.cmdArgs.join(' ') == '-k gmake -j 2 clean install'
}

def "It is not necessary to set makefile"() {
given:
project.allprojects {
make {
executable 'foo'
targets 'clean'
}
}

project.tasks.make.executor = new FakeExecutor()
project.evaluate()
project.tasks.make.execute()

expect:
project.tasks.make.didWork
}

def "Monitor input sources and output folders"() {
given:
File srcDir = new File('src').absoluteFile
Expand Down
60 changes: 60 additions & 0 deletions src/test/groovy/org/ysb33r/gradle/gnumake/RunMakeSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// ============================================================================
// (C) Copyright Schalk W. Cronje 2013-2015
//
// This software is licensed under the Apache License 2.0
// See http://www.apache.org/licenses/LICENSE-2.0 for license details
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.
//
// ============================================================================
//

package org.ysb33r.gradle.gnumake

import org.gradle.api.Project
import org.gradle.internal.os.OperatingSystem
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification


/**
* @author Schalk W. Cronjé
*/
class RunMakeSpec extends Specification {

static final File MAKESCRIPT = new File( "src/test/resources/fake-make-scripts/${OperatingSystem.current().isWindows() ? 'fake-make.bat' : 'fake-make.sh'}" )
Project project = ProjectBuilder.builder().build()

void captureStdOut() {
}


def "Check that Make is invoked with the correct arguments"() {
given:
def systemOut = new ByteArrayOutputStream()
System.out = new PrintStream(systemOut)

project.allprojects {
apply plugin : 'org.ysb33r.gnumake'

gnumake {
executable MAKESCRIPT.absolutePath
}

make {
flags DESTDIR : 'foo/bar'
targets 'build','install'
}
}

project.evaluate()
project.tasks.make.execute()
String output = systemOut.toString()

expect:
output.contains('fake-make build install DESTDIR=foo/bar')
}
}
2 changes: 1 addition & 1 deletion src/test/resources/fake-make-scripts/fake-make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
@REM

@echo off
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
echo fake-make %1 %2 %3 %4 %5 %6 %7 %8 %9
2 changes: 1 addition & 1 deletion src/test/resources/fake-make-scripts/fake-make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# ============================================================================
#

echo $@
echo fake-make $@

0 comments on commit 4b0fed8

Please sign in to comment.