Skip to content

Commit 187fbec

Browse files
committed
support build native activity
1 parent e31eee2 commit 187fbec

File tree

9 files changed

+97
-124
lines changed

9 files changed

+97
-124
lines changed

app/src/main/java/com/duy/ccppcompiler/compiler/CompileManagerImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void onPrepareCompile() {
6060
mDiagnosticPresenter.clear();
6161
}
6262
mActivity.setMenuStatus(R.id.action_run, MenuDef.STATUS_DISABLED);
63+
mActivity.setMenuStatus(R.id.action_build_native_activity, MenuDef.STATUS_DISABLED);
6364

6465
mCompileDialog = new ProgressDialog(mActivity);
6566
mCompileDialog.setTitle(mActivity.getString(R.string.compiling));
@@ -105,7 +106,10 @@ public void setDiagnosticPresenter(DiagnosticPresenter diagnosticPresenter) {
105106

106107
private void finishCompile(T commandResult) {
107108
hideDialog();
109+
108110
mActivity.setMenuStatus(R.id.action_run, MenuDef.STATUS_NORMAL);
111+
mActivity.setMenuStatus(R.id.action_build_native_activity, MenuDef.STATUS_NORMAL);
112+
109113
if (mDiagnosticPresenter != null) {
110114
DiagnosticsCollector diagnosticsCollector = new DiagnosticsCollector();
111115
OutputParser parser = new OutputParser(diagnosticsCollector);

app/src/main/java/com/duy/ccppcompiler/compiler/CompileSetting.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public String getCFlags() {
4444
String cFlags = mPref.getString(mContext.getString(R.string.pref_key_c_options), "");
4545
builder.addFlags(cFlags);
4646

47-
return builder.buildCommand();
47+
return builder.build();
4848
}
4949

5050
@Override
@@ -55,7 +55,7 @@ public String getCxxFlags() {
5555
String cxxFlags = mPref.getString(mContext.getString(R.string.pref_key_cxx_options), "");
5656
builder.addFlags(cxxFlags);
5757

58-
return builder.buildCommand();
58+
return builder.build();
5959

6060
}
6161

@@ -107,7 +107,7 @@ private String getGCCFlags() {
107107
boolean Werror = mPref.getBoolean(mContext.getString(R.string.pref_option_werror), false);
108108
if (Werror) builder.addFlags("-Werror");
109109

110-
return builder.buildCommand();
110+
return builder.build();
111111
}
112112

113113
}

app/src/main/java/com/duy/ccppcompiler/compiler/compilers/CompilerFactory.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
import java.io.File;
2727

28-
import static com.duy.ccppcompiler.compiler.compilers.CompilerFactory.CompileType.NATIVE_ACTIVITY;
29-
3028
/**
3129
* Created by Duy on 25-Apr-18.
3230
*/
@@ -39,9 +37,7 @@ public static ICompiler getCompilerForFile(Context context, File[] sourceFiles,
3937
String fileName = file.getName();
4038

4139
CompilerFactory.CompileType compilerType = CompileType.NONE;
42-
if (nativeActivity) {
43-
compilerType = NATIVE_ACTIVITY;
44-
} else if (Catalog.getModeByName("C++").acceptFile(filePath, fileName)) {
40+
if (Catalog.getModeByName("C++").acceptFile(filePath, fileName)) {
4541
compilerType = CompilerFactory.CompileType.G_PLUS_PLUS;
4642

4743
} else if (Catalog.getModeByName("C").acceptFile(filePath, fileName)) {
@@ -51,13 +47,10 @@ public static ICompiler getCompilerForFile(Context context, File[] sourceFiles,
5147

5248
switch (compilerType) {
5349
case G_PLUS_PLUS:
54-
return new GPlusPlusCompiler(context, new CompileSetting(context));
50+
return new GPlusPlusCompiler(context, nativeActivity, new CompileSetting(context));
5551

5652
case GCC:
57-
return new GCCCompiler(context, new CompileSetting(context));
58-
59-
case NATIVE_ACTIVITY:
60-
return new NativeActivityCompiler(context, new CompileSetting(context));
53+
return new GCCCompiler(context, nativeActivity, new CompileSetting(context));
6154

6255
default:
6356
return null;
@@ -69,6 +62,6 @@ public static ICompiler getCompilerForFile(Context context, File[] sourceFiles,
6962
*/
7063

7164
public enum CompileType {
72-
GCC, G_PLUS_PLUS, NATIVE_ACTIVITY, NONE
65+
GCC, G_PLUS_PLUS, NONE
7366
}
7467
}

app/src/main/java/com/duy/ccppcompiler/compiler/compilers/CompilerImpl.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,46 @@
1919
import android.content.Context;
2020
import android.support.annotation.NonNull;
2121

22+
import com.duy.ccppcompiler.compiler.shell.CommandBuilder;
2223
import com.duy.ccppcompiler.compiler.shell.CommandResult;
2324
import com.duy.ccppcompiler.compiler.shell.Shell;
2425

26+
import java.io.File;
27+
2528
/**
2629
* Created by Duy on 18-May-18.
2730
*/
28-
public abstract class CompilerImpl<T extends CommandResult> implements ICompiler<T> {
29-
private static final String TAG = "NativeCompilerImpl";
31+
public abstract class CompilerImpl implements ICompiler {
32+
static final int RESULT_NO_ERROR = 0;
33+
34+
Context mContext;
35+
36+
CompilerImpl(Context context) {
37+
this.mContext = context;
38+
}
3039

3140
@NonNull
32-
protected CommandResult execCommand(@NonNull Context context, @NonNull String workingDir, @NonNull String cmd) {
41+
private CommandResult execCommand(@NonNull Context context, @NonNull String workingDir, @NonNull String cmd) {
3342
return Shell.exec(context, workingDir, cmd);
3443
}
3544

3645
@Override
37-
public void hangup() {
46+
public CommandResult compile(File[] sourceFiles) {
47+
clean();
48+
49+
File sourceFile = sourceFiles[0];
50+
CommandBuilder command = new CommandBuilder(getCompilerProgram());
51+
String args = buildArgs(sourceFiles);
52+
command.addFlags(args);
53+
return execCommand(mContext, sourceFile.getParent(), command.build());
54+
}
55+
56+
protected void clean() {
3857

3958
}
59+
60+
protected abstract String buildArgs(File[] sourceFiles);
61+
62+
protected abstract String getCompilerProgram();
63+
4064
}

app/src/main/java/com/duy/ccppcompiler/compiler/compilers/GCCCompiler.java

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,26 @@
3636
* Created by Duy on 25-Apr-18.
3737
*/
3838

39-
public class GCCCompiler extends CompilerImpl<GccCompileResult> {
39+
public class GCCCompiler extends CompilerImpl {
4040
private static final String TAG = "GCCCompiler";
41-
private static final String COMPILER_NAME = "gcc-4.9";
41+
private static final String GCC_COMPILER_NAME = "gcc-4.9";
42+
4243
protected File mOutFile;
43-
protected Context mContext;
44-
@Nullable
4544
protected ICompileSetting mSetting;
4645

47-
public GCCCompiler(Context context, @Nullable ICompileSetting compileSetting) {
48-
mContext = context;
49-
mSetting = compileSetting;
46+
private boolean mBuildNativeActivity;
47+
48+
public GCCCompiler(Context context, boolean nativeActivity, @Nullable ICompileSetting setting) {
49+
super(context);
50+
mBuildNativeActivity = nativeActivity;
51+
mSetting = setting;
5052
}
5153

5254
@Override
5355
public GccCompileResult compile(File[] sourceFiles) {
54-
File sourceFile = sourceFiles[0];
55-
String command = buildCommand(sourceFiles);
56-
57-
GccCompileResult result = new GccCompileResult(execCommand(mContext, sourceFile.getParent(), command));
58-
if (result.getResultCode() == 0) {
59-
if (mOutFile.exists()) {
56+
GccCompileResult result = new GccCompileResult(super.compile(sourceFiles));
57+
if (result.getResultCode() == RESULT_NO_ERROR) {
58+
if (mOutFile != null && mOutFile.exists()) {
6059
try {
6160
File internalBinary = new File(Environment.getTmpExeDir(mContext), mOutFile.getName());
6261
FileInputStream inputStream = new FileInputStream(mOutFile);
@@ -65,66 +64,62 @@ public GccCompileResult compile(File[] sourceFiles) {
6564
inputStream.close();
6665
outputStream.close();
6766

67+
//set executable
6868
Utils.chmod(internalBinary.getAbsolutePath(), 0x1ed/*0775*/);
6969

7070
result.setBinaryFile(internalBinary);
71+
7172
} catch (Exception e) {
7273
e.printStackTrace();
7374
result.setResultCode(-1);
74-
result.setMessage("Application error: " + e.getMessage() + ". Please report bug on GitHub of project");
75+
result.setMessage("Application error: " + e.getMessage() +
76+
". Please report bug on GitHub of project");
7577
}
7678
}
7779
}
7880
return result;
7981
}
8082

81-
protected String buildCommand(File[] sourceFiles) {
82-
File source = sourceFiles[0];
83-
String fileName = source.getName();
83+
@Override
84+
protected String getCompilerProgram() {
85+
return GCC_COMPILER_NAME;
86+
}
8487

85-
mOutFile = new File(getBuildDir(source), fileName.substring(0, fileName.lastIndexOf(".")));
88+
@Override
89+
protected String buildArgs(File[] sourceFiles) {
90+
File source = sourceFiles[0];
91+
String nameNoExt = source.getName().substring(0, source.getName().lastIndexOf("."));
8692

87-
CommandBuilder builder = new CommandBuilder(COMPILER_NAME);
93+
CommandBuilder args = new CommandBuilder();
8894
for (File sourceFile : sourceFiles) {
89-
builder.addFlags(sourceFile.getAbsolutePath());
95+
args.addFlags(sourceFile.getAbsolutePath());
9096
}
9197
if (mSetting != null) {
92-
builder.addFlags(mSetting.getCFlags());
98+
args.addFlags(mSetting.getCFlags());
99+
}
100+
101+
if (mBuildNativeActivity) {
102+
mOutFile = new File(source.getParent(), "lib" + nameNoExt + ".so");
103+
String cCtoolsDir = Environment.getCCtoolsDir(mContext);
104+
args.addFlags(String.format("-I%s/sources/native_app_glue", cCtoolsDir))
105+
.addFlags(String.format("%s/sources/native_app_glue/android_native_app_glue.c", cCtoolsDir))
106+
.addFlags(String.format("-Wl,-soname,%s", mOutFile.getAbsolutePath()))
107+
.addFlags("-shared")
108+
.addFlags("-Wl,--no-undefined -Wl,-z,noexecstack")
109+
.addFlags("-llog")
110+
.addFlags("-landroid")
111+
.addFlags("-lm")
112+
.addFlags("-o", mOutFile.getAbsolutePath());
113+
} else {
114+
mOutFile = new File(source.getParent(), nameNoExt);
115+
args.addFlags("-o", mOutFile.getAbsolutePath());
93116
}
94-
builder.addFlags("-o", mOutFile.getAbsolutePath());
95117

96118
if (Build.VERSION.SDK_INT >= 21) {
97-
builder.addFlags("-pie");
119+
args.addFlags("-pie");
98120
}
99-
// builder.addFlags("-std=c99");
100-
// builder.addFlags("-lz"/*zlib*/, "-ldl", "-lm" /*math*/, "-llog");
101-
// builder.addFlags("-lncurses");
102-
// builder.addFlags("-Og");
103-
104-
// Emit fix-it hints in a machine-parseable format, suitable for consumption by IDEs.
105-
// For each fix-it, a line will be printed after the relevant diagnostic, starting with the
106-
// string “fix-it:”. For example:
107-
// fix-it:"test.c":{45:3-45:21}:"gtk_widget_show_all"
108-
// builder.add("-fdiagnostics-parseable-fixits");
109-
110-
// By default, each diagnostic emitted includes text indicating the command-line option that
111-
// directly controls the diagnostic (if such an option is known to the diagnostic machinery).
112-
// Specifying the -fno-diagnostics-show-option flag suppresses that behavior.
113-
//builder.addFlags("-fno-diagnostics-show-option");
114-
115-
// By default, each diagnostic emitted includes the original source line and a caret ‘^’
116-
// indicating the column. This option suppresses this information. The source line is
117-
// truncated to n characters, if the -fmessage-length=n option is given. When the output is
118-
// done to the terminal, the width is limited to the width given by the COLUMNS environment
119-
// variable or, if not set, to the terminal width.
120-
//builder.addFlags("-fno-diagnostics-show-caret");
121-
122-
return builder.buildCommand();
123-
}
124121

125-
protected String getBuildDir(File source) {
126-
File buildDir = new File(source.getParent(), "build");
127-
buildDir.mkdir();
128-
return buildDir.getAbsolutePath();
122+
return args.build();
129123
}
124+
130125
}

app/src/main/java/com/duy/ccppcompiler/compiler/compilers/GPlusPlusCompiler.java

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,22 @@
1717
package com.duy.ccppcompiler.compiler.compilers;
1818

1919
import android.content.Context;
20-
import android.os.Build;
2120

2221
import com.duy.ccppcompiler.compiler.ICompileSetting;
23-
import com.duy.ccppcompiler.compiler.shell.CommandBuilder;
24-
import com.duy.ccppcompiler.compiler.shell.GccCompileResult;
25-
26-
import java.io.File;
2722

2823
/**
24+
* G++ compiler as GCC compiler but it different name, just override {@link #getCompilerProgram()}
2925
* Created by Duy on 25-Apr-18.
3026
*/
3127
public class GPlusPlusCompiler extends GCCCompiler {
32-
private static final String TAG = "GPlusPlusCompiler";
33-
private static final String COMPILER_NAME = "g++-4.9";
28+
private static final String G_PLUS_PLUS_COMPILER_NAME = "g++-4.9";
3429

35-
public GPlusPlusCompiler(Context context, ICompileSetting compileSetting) {
36-
super(context, compileSetting);
30+
public GPlusPlusCompiler(Context context, boolean nativeActivity, ICompileSetting setting) {
31+
super(context, nativeActivity, setting);
3732
}
3833

3934
@Override
40-
public GccCompileResult compile(File[] sourceFiles) {
41-
return super.compile(sourceFiles);
42-
}
43-
44-
@Override
45-
protected String buildCommand(File[] sourceFiles) {
46-
File file = sourceFiles[0];
47-
String fileName = file.getName();
48-
mOutFile = new File(getBuildDir(file), fileName.substring(0, fileName.lastIndexOf(".")));
49-
50-
CommandBuilder builder = new CommandBuilder(COMPILER_NAME);
51-
for (File sourceFile : sourceFiles) {
52-
builder.addFlags(sourceFile.getAbsolutePath());
53-
}
54-
builder.addFlags("-o", mOutFile.getAbsolutePath());
55-
if (mSetting != null) {
56-
builder.addFlags(mSetting.getCxxFlags());
57-
}
58-
59-
if (Build.VERSION.SDK_INT >= 21) {
60-
builder.addFlags("-pie");
61-
}
62-
// builder.addFlags("-std=c++14");
63-
// builder.addFlags("-lz", "-ldl", "-lm", "-llog", "-Og");
64-
65-
66-
// By default, each diagnostic emitted includes text indicating the command-line option that
67-
// directly controls the diagnostic (if such an option is known to the diagnostic machinery).
68-
// Specifying the -fno-diagnostics-show-option flag suppresses that behavior.
69-
builder.addFlags("-fno-diagnostics-show-option");
70-
71-
// By default, each diagnostic emitted includes the original source line and a caret ‘^’
72-
// indicating the column. This option suppresses this information. The source line is
73-
// truncated to n characters, if the -fmessage-length=n option is given. When the output is
74-
// done to the terminal, the width is limited to the width given by the COLUMNS environment
75-
// variable or, if not set, to the terminal width.
76-
//builder.addFlags("-fno-diagnostics-show-caret");
77-
78-
return builder.buildCommand();
35+
protected String getCompilerProgram() {
36+
return G_PLUS_PLUS_COMPILER_NAME;
7937
}
8038
}

app/src/main/java/com/duy/ccppcompiler/compiler/compilers/ICompiler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
* Created by Duy on 25-Apr-18.
2727
*/
2828

29-
public interface ICompiler<T extends CommandResult> {
29+
public interface ICompiler {
3030
@WorkerThread
31-
T compile(File[] sourceFiles) ;
31+
CommandResult compile(File[] sourceFiles);
3232

33-
void hangup();
3433
}

app/src/main/java/com/duy/ccppcompiler/compiler/shell/CommandBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void addFlags(CommandBuilder otherFlags) {
5353
}
5454

5555
@NonNull
56-
public String buildCommand() {
56+
public String build() {
5757
StringBuilder cmd = new StringBuilder();
5858
cmd.append(program);
5959
if (flags.size() == 0) {

app/src/main/java/com/duy/ccppcompiler/ui/InstallActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ protected Boolean doInBackground(Void... voids) {
192192
IOUtils.write(/*"#include <stdio.h>\n" +*/ "int main(){ return 0; }", output);
193193
output.close();
194194

195-
GCCCompiler compiler = new GCCCompiler(context, null);
195+
GCCCompiler compiler = new GCCCompiler(context, nativeActivity, null);
196196
GccCompileResult result = compiler.compile(new File[]{file});
197197
if (result == null || result.getResultCode() != 0) {
198198
publishProgress("Could not exec C compiler, please install compiler");
@@ -204,7 +204,7 @@ protected Boolean doInBackground(Void... voids) {
204204
output = new FileOutputStream(file);
205205
IOUtils.write(/*"#include <iostream>\n" + "using namespace std;\n" +*/ "int main() { return 0; }", output);
206206
output.close();
207-
compiler = new GPlusPlusCompiler(context, null);
207+
compiler = new GPlusPlusCompiler(context, nativeActivity, null);
208208
result = compiler.compile(new File[]{file});
209209
if (result == null || result.getResultCode() != 0) {
210210
publishProgress("Could not exec C++ compiler, please install compiler");

0 commit comments

Comments
 (0)