Skip to content

Commit f00c8bb

Browse files
author
Joshua Kennedy
committed
added the ability to pass argument to scan-build
1 parent da2c7c4 commit f00c8bb

File tree

8 files changed

+93
-8
lines changed

8 files changed

+93
-8
lines changed

src/main/java/jenkins/plugins/clangscanbuild/ClangScanBuildBuilder.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package jenkins.plugins.clangscanbuild;
22

3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
37
import hudson.Extension;
48
import hudson.FilePath;
59
import hudson.Launcher;
610
import hudson.Util;
711
import hudson.model.BuildListener;
812
import hudson.model.AbstractBuild;
913
import hudson.tasks.Builder;
14+
import hudson.util.FormValidation;
1015
import jenkins.plugins.clangscanbuild.commands.BuildContextImpl;
1116
import jenkins.plugins.clangscanbuild.commands.ScanBuildCommand;
1217

1318
import org.kohsuke.stapler.DataBoundConstructor;
19+
import org.kohsuke.stapler.QueryParameter;
1420

1521
/**
1622
* This builder provides a new build step for freestyle jobs. Users can
@@ -35,6 +41,7 @@ public class ClangScanBuildBuilder extends Builder{
3541
private String xcodeProjectSubPath;
3642
private String workspace;
3743
private String scheme;
44+
private String scanbuildargs;
3845

3946
@DataBoundConstructor
4047
public ClangScanBuildBuilder(
@@ -44,7 +51,8 @@ public ClangScanBuildBuilder(
4451
String clangInstallationName,
4552
String xcodeProjectSubPath,
4653
String workspace,
47-
String scheme ){
54+
String scheme,
55+
String scanbuildargs ){
4856

4957
this.target = Util.fixEmptyAndTrim( target );
5058
this.targetSdk = Util.fixEmptyAndTrim( targetSdk );
@@ -53,6 +61,7 @@ public ClangScanBuildBuilder(
5361
this.xcodeProjectSubPath = Util.fixEmptyAndTrim( xcodeProjectSubPath );
5462
this.workspace = Util.fixEmptyAndTrim( workspace );
5563
this.scheme = Util.fixEmptyAndTrim( scheme );
64+
this.scanbuildargs = Util.fixEmptyAndTrim( scanbuildargs );
5665
}
5766

5867
public String getClangInstallationName(){
@@ -79,6 +88,10 @@ public String getScheme(){
7988
return scheme;
8089
}
8190

91+
public String getScanbuildargs(){
92+
return scanbuildargs;
93+
}
94+
8295
/**
8396
* Removing slashes here in case the user adds a starting slash to the path.
8497
*/
@@ -89,7 +102,7 @@ public String getXcodeProjectSubPath(){
89102
}
90103
return xcodeProjectSubPath;
91104
}
92-
105+
93106
/**
94107
* This method is invoked when a job is actually executed. It is the magic method.
95108
* @return boolean - if 'false', build will be aborted
@@ -109,6 +122,7 @@ public boolean perform( @SuppressWarnings("rawtypes") AbstractBuild build, Launc
109122
xcodebuild.setTarget( getTarget() );
110123
xcodebuild.setTargetSdk( getTargetSdk() );
111124
xcodebuild.setConfig( getConfig() );
125+
xcodebuild.setAdditionalScanBuildArguments( getScanbuildargs() );
112126
xcodebuild.setClangOutputFolder( new FilePath( build.getWorkspace(), ClangScanBuildUtils.REPORT_OUTPUT_FOLDERNAME) );
113127
xcodebuild.setWorkspace( getWorkspace() );
114128
xcodebuild.setScheme( getScheme() );

src/main/java/jenkins/plugins/clangscanbuild/ClangScanBuildDescriptor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public void setInstallations( ClangScanBuildToolInstallation[] clangInstallation
6464
save();
6565
}
6666

67-
// TARGET SDK
6867
public FormValidation doCheckTargetSdk( @QueryParameter String value ) throws IOException, ServletException {
6968
if( value.length() == 0 ) return FormValidation.error( "You must provide a target SDK. You can execute 'xcodebuild -showsdks' from Terminal.app to see allowed values." );
7069
return FormValidation.ok();

src/main/java/jenkins/plugins/clangscanbuild/actions/ClangScanBuildAction.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
import hudson.model.Action;
55
import hudson.model.ModelObject;
66
import hudson.model.AbstractBuild;
7+
import hudson.util.FormValidation;
78

89
import java.io.IOException;
910
import java.util.regex.Pattern;
1011

12+
import javax.servlet.ServletException;
13+
1114
import jenkins.plugins.clangscanbuild.ClangScanBuildUtils;
1215
import jenkins.plugins.clangscanbuild.history.ClangScanBuildBugSummary;
1316

17+
import org.kohsuke.stapler.QueryParameter;
1418
import org.kohsuke.stapler.StaplerProxy;
1519
import org.kohsuke.stapler.StaplerRequest;
1620
import org.kohsuke.stapler.StaplerResponse;
@@ -120,8 +124,6 @@ public Object getTarget(){
120124
return this;
121125
}
122126

123-
124-
125127
/**
126128
* This method is used to serve up report HTML files from the hidden build folder. It essentially exposes
127129
* the reports to the web.

src/main/java/jenkins/plugins/clangscanbuild/commands/ScanBuildCommand.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class ScanBuildCommand implements Command{
1515

1616
private String target;
1717

18+
private String additionalScanBuildArguments; // Passed directly to shell
19+
1820
private String workspace;
1921
private String scheme;
2022

@@ -30,12 +32,24 @@ public int execute( BuildContext context ) throws Exception {
3032

3133
ArgumentListBuilder args = new ArgumentListBuilder();
3234
args.add( getClangScanBuildPath() );
35+
3336
args.add( "-k" ); // keep going on failure
3437
args.add( "-v" ); // verbose
3538
args.add( "-v" ); // even more verbose
39+
3640
args.add( "-o" ); // output folder
3741
args.add( "" + clangOutputFolder.getRemote().replaceAll( " ", "/ " ) + "" );
3842

43+
String additionalArgs = getAdditionalScanBuildArguments();
44+
if( isNotBlank( additionalArgs ) ){
45+
// This is a hack. I can't call the standard ArgumentListBuilder.add() method because it checks for spaces with-in
46+
// the arg and quotes the arg if a space exists. Since user's can pass commands like
47+
// '--use-cc=`which clang`' or multiple commands...we cannot allow the quotes to be
48+
// inserted when spaces exist. The ArgumentListBuilder.addTokenized() splits the arg on spaces and adds each piece
49+
// which ends up reinserting the spaces when the command is assembled.
50+
args.addTokenized( additionalArgs );
51+
}
52+
3953
args.add( "xcodebuild" );
4054

4155
if( isNotBlank( getWorkspace() ) ){
@@ -147,4 +161,12 @@ public void setScheme(String scheme) {
147161
this.scheme = scheme;
148162
}
149163

164+
public String getAdditionalScanBuildArguments() {
165+
return additionalScanBuildArguments;
166+
}
167+
168+
public void setAdditionalScanBuildArguments(String additionalScanBuildArguments) {
169+
this.additionalScanBuildArguments = additionalScanBuildArguments;
170+
}
171+
150172
}

src/main/resources/jenkins/plugins/clangscanbuild/ClangScanBuildBuilder/config.jelly

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<f:textbox default="" />
3535
</f:entry>
3636

37+
<f:entry title="Additional scan-build arguments" field="scanbuildargs">
38+
<f:textbox default="" />
39+
</f:entry>
40+
3741
</f:advanced>
3842

3943
</j:jelly>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This field can be used to pass additional arguments to clang scan-build. The arguments will be inserted into the final command with a space before
2+
and after what you type in this field. The arguments will appear before the xcodebuild sub command. You can view the assembled command by viewing the
3+
job's build console.

src/test/java/jenkins/plugins/clangscanbuild/ClangScanBuildBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ public void testRoundTripConfiguration() throws Exception{
1414

1515
FreeStyleProject p = createFreeStyleProject();
1616

17-
ClangScanBuildBuilder builderBefore = new ClangScanBuildBuilder( "target", "sdk", "config", "installName", "projPath", "workspace", "scheme" );
17+
ClangScanBuildBuilder builderBefore = new ClangScanBuildBuilder( "target", "sdk", "config", "installName", "projPath", "workspace", "scheme", "someargs" );
1818
p.getBuildersList().add( builderBefore );
1919

2020
HtmlForm form = createWebClient().getPage( p, "configure" ).getFormByName( "config" );
2121
submit( form );
2222

2323
ClangScanBuildBuilder builderAfter = p.getBuildersList().get( ClangScanBuildBuilder.class );
2424

25-
assertEqualBeans( builderBefore, builderAfter, "target,config,targetSdk,xcodeProjectSubPath,workspace,scheme" );
25+
assertEqualBeans( builderBefore, builderAfter, "target,config,targetSdk,xcodeProjectSubPath,workspace,scheme,scanbuildargs" );
2626
}
2727

2828
}

src/test/java/jenkins/plugins/clangscanbuild/commands/ScanBuildCommandTest.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void xcode4WorkspaceSet() throws Exception{
4040
command.setTarget( "myTarget" );
4141
command.setTargetSdk( "myTargetSdk" );
4242
command.setWorkspace( "myWorkspace" );
43-
43+
4444
String actual = buildCommandAndReturn( command );
4545

4646
String expected = "/ScanBuild -k -v -v -o OutputFolder xcodebuild -workspace myWorkspace -scheme myScheme -configuration myConfig -sdk myTargetSdk clean build";
@@ -78,4 +78,45 @@ private String buildCommandAndReturn( ScanBuildCommand command ) throws Exceptio
7878
return argumentListCapture.getValue().toStringWithQuote();
7979
}
8080

81+
@Test
82+
public void xcode4WorkspaceSetWithSingleScanBuildArgument() throws Exception{
83+
// XCode 4 workspace/scheme should override unnecessary target
84+
ScanBuildCommand command = new ScanBuildCommand();
85+
command.setClangOutputFolder( new FilePath( new File( "OutputFolder" ) ) );
86+
command.setClangScanBuildPath( "/ScanBuild" );
87+
command.setConfig( "myConfig" );
88+
command.setProjectDirectory( new FilePath( new File( "/ProjectDir" ) ) );
89+
command.setScheme( "myScheme" );
90+
command.setTarget( "myTarget" );
91+
command.setTargetSdk( "myTargetSdk" );
92+
command.setWorkspace( "myWorkspace" );
93+
command.setAdditionalScanBuildArguments( "--use-cc=`which clang`" );
94+
95+
String actual = buildCommandAndReturn( command );
96+
97+
// Jenkins core quotes this due to the space in between 'which' and 'clang' . Not sure if this is OK or not... :(
98+
String expected = "/ScanBuild -k -v -v -o OutputFolder --use-cc=`which clang` xcodebuild -workspace myWorkspace -scheme myScheme -configuration myConfig -sdk myTargetSdk clean build";
99+
Assert.assertEquals( expected, actual );
100+
}
101+
102+
@Test
103+
public void xcode4WorkspaceSetWithMultipleScanBuildArguments() throws Exception{
104+
// XCode 4 workspace/scheme should override unnecessary target
105+
ScanBuildCommand command = new ScanBuildCommand();
106+
command.setClangOutputFolder( new FilePath( new File( "OutputFolder" ) ) );
107+
command.setClangScanBuildPath( "/ScanBuild" );
108+
command.setConfig( "myConfig" );
109+
command.setProjectDirectory( new FilePath( new File( "/ProjectDir" ) ) );
110+
command.setScheme( "myScheme" );
111+
command.setTarget( "myTarget" );
112+
command.setTargetSdk( "myTargetSdk" );
113+
command.setWorkspace( "myWorkspace" );
114+
command.setAdditionalScanBuildArguments( "-h -x somevalue" );
115+
116+
String actual = buildCommandAndReturn( command );
117+
118+
String expected = "/ScanBuild -k -v -v -o OutputFolder -h -x somevalue xcodebuild -workspace myWorkspace -scheme myScheme -configuration myConfig -sdk myTargetSdk clean build";
119+
Assert.assertEquals( expected, actual );
120+
}
121+
81122
}

0 commit comments

Comments
 (0)