Skip to content

Commit 8a03151

Browse files
committed
Implement build SDL for g++ compiler
1 parent b697648 commit 8a03151

File tree

10 files changed

+180
-83
lines changed

10 files changed

+180
-83
lines changed

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

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
public class CompilerFactory {
3333
@Nullable
34-
public static ICompiler getCompilerForFile(Context context, File[] sourceFiles, boolean nativeActivity) {
34+
public static ICompiler getCompilerForFile(Context context, File[] sourceFiles) {
3535
File file = sourceFiles[0];
3636
String filePath = file.getAbsolutePath();
3737
String fileName = file.getName();
@@ -42,18 +42,84 @@ public static ICompiler getCompilerForFile(Context context, File[] sourceFiles,
4242

4343
} else if (Catalog.getModeByName("C").acceptFile(filePath, fileName)) {
4444
compilerType = CompileType.GCC;
45-
} else if (Catalog.getModeByName("Makefile").acceptFile(filePath, fileName)){
45+
} else if (Catalog.getModeByName("Makefile").acceptFile(filePath, fileName)) {
4646

4747
compilerType = CompileType.MAKE;
4848
}
4949

5050

5151
switch (compilerType) {
5252
case G_PLUS_PLUS:
53-
return new GPlusPlusCompiler(context, nativeActivity, new CompileSetting(context));
53+
return new GPlusPlusCompiler(context, false, false, new CompileSetting(context));
5454

5555
case GCC:
56-
return new GCCCompiler(context, nativeActivity, new CompileSetting(context));
56+
return new GCCCompiler(context, false, false, new CompileSetting(context));
57+
58+
case MAKE:
59+
return new MakeCompiler(context);
60+
61+
default:
62+
return null;
63+
}
64+
}
65+
66+
@Nullable
67+
public static ICompiler getNativeActivityCompilerForFile(Context context, File[] sourceFiles) {
68+
File file = sourceFiles[0];
69+
String filePath = file.getAbsolutePath();
70+
String fileName = file.getName();
71+
72+
CompileType compilerType = CompileType.NONE;
73+
if (Catalog.getModeByName("C++").acceptFile(filePath, fileName)) {
74+
compilerType = CompileType.G_PLUS_PLUS;
75+
76+
} else if (Catalog.getModeByName("C").acceptFile(filePath, fileName)) {
77+
compilerType = CompileType.GCC;
78+
} else if (Catalog.getModeByName("Makefile").acceptFile(filePath, fileName)) {
79+
80+
compilerType = CompileType.MAKE;
81+
}
82+
83+
84+
switch (compilerType) {
85+
case G_PLUS_PLUS:
86+
return new GPlusPlusCompiler(context, true, false, new CompileSetting(context));
87+
88+
case GCC:
89+
return new GCCCompiler(context, true, false, new CompileSetting(context));
90+
91+
case MAKE:
92+
return new MakeCompiler(context);
93+
94+
default:
95+
return null;
96+
}
97+
}
98+
99+
@Nullable
100+
public static ICompiler getSDLActivityCompilerForFile(Context context, File[] sourceFiles) {
101+
File file = sourceFiles[0];
102+
String filePath = file.getAbsolutePath();
103+
String fileName = file.getName();
104+
105+
CompileType compilerType = CompileType.NONE;
106+
if (Catalog.getModeByName("C++").acceptFile(filePath, fileName)) {
107+
compilerType = CompileType.G_PLUS_PLUS;
108+
109+
} else if (Catalog.getModeByName("C").acceptFile(filePath, fileName)) {
110+
compilerType = CompileType.GCC;
111+
} else if (Catalog.getModeByName("Makefile").acceptFile(filePath, fileName)) {
112+
113+
compilerType = CompileType.MAKE;
114+
}
115+
116+
117+
switch (compilerType) {
118+
case G_PLUS_PLUS:
119+
return new GPlusPlusCompiler(context, false, true, new CompileSetting(context));
120+
121+
case GCC:
122+
return new GCCCompiler(context, false, true, new CompileSetting(context));
57123

58124
case MAKE:
59125
return new MakeCompiler(context);

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

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.duy.common.DLog;
2929

3030
import java.io.File;
31+
import java.util.ArrayList;
3132

3233
/**
3334
* Created by Duy on 25-Apr-18.
@@ -36,14 +37,17 @@
3637
public class GCCCompiler extends CompilerImpl {
3738
private static final String TAG = "GCCCompiler";
3839
private static final String GCC_COMPILER_NAME = "gcc";
40+
protected final ICompileSetting mSetting;
41+
42+
protected final boolean mBuildNativeActivity;
43+
protected final boolean mBuildSDL;
3944

4045
File mOutFile;
41-
ICompileSetting mSetting;
42-
boolean mBuildNativeActivity;
4346

44-
public GCCCompiler(Context context, boolean nativeActivity, @Nullable ICompileSetting setting) {
47+
public GCCCompiler(Context context, boolean nativeActivity, boolean buildSDL, @Nullable ICompileSetting setting) {
4548
super(context);
4649
mBuildNativeActivity = nativeActivity;
50+
mBuildSDL = buildSDL;
4751
mSetting = setting;
4852
}
4953

@@ -71,35 +75,17 @@ protected String getCompilerProgram() {
7175

7276
@Override
7377
protected String buildArgs(File[] sourceFiles) {
74-
File source = sourceFiles[0];
75-
String nameNoExt = source.getName().substring(0, source.getName().lastIndexOf("."));
76-
7778
CommandBuilder args = new CommandBuilder();
7879
for (File sourceFile : sourceFiles) {
7980
args.addFlags(sourceFile.getAbsolutePath());
8081
}
8182

8283
if (mBuildNativeActivity) {
83-
if (DLog.DEBUG) DLog.d(TAG, "buildArgs: build native activity");
84-
String soName = "lib" + nameNoExt + ".so";
85-
mOutFile = new File(source.getParent(), soName);
86-
87-
String cctools = Environment.getCCtoolsDir(mContext);
88-
89-
args.addFlags("-I" + cctools + "/sources/native_app_glue/")
90-
.addFlags(cctools + "/sources/native_app_glue/android_native_app_glue.c")
91-
.addFlags("-Wl,-soname," + soName)
92-
.addFlags("-shared")
93-
.addFlags("-Wl,--no-undefined")
94-
.addFlags("-Wl,-z,noexecstack")
95-
.addFlags("-llog") //lib log
96-
.addFlags("-landroid") //android
97-
.addFlags("-lm")
98-
.addFlags("-o", mOutFile.getAbsolutePath())
99-
;
84+
args.addFlags(buildNativeActivityFlags(sourceFiles));
85+
} else if (mBuildSDL) {
86+
args.addFlags(buildSDLActivityFlags(sourceFiles));
10087
} else {
101-
mOutFile = new File(source.getParent(), nameNoExt);
102-
args.addFlags("-o", mOutFile.getAbsolutePath());
88+
args.addFlags(buildExecutableFlags(sourceFiles));
10389
}
10490

10591
if (mSetting != null) {
@@ -110,4 +96,63 @@ protected String buildArgs(File[] sourceFiles) {
11096
return args.build();
11197
}
11298

99+
private ArrayList<String> buildSDLActivityFlags(File[] sourceFiles) {
100+
File source = sourceFiles[0];
101+
String nameNoExt = source.getName().substring(0, source.getName().lastIndexOf("."));
102+
103+
if (DLog.DEBUG) DLog.d(TAG, "buildArgs: build native activity");
104+
String soName = "lib" + nameNoExt + ".so";
105+
mOutFile = new File(source.getParent(), soName);
106+
String cctools = Environment.getCCtoolsDir(mContext);
107+
CommandBuilder flags = new CommandBuilder();
108+
109+
//default flags
110+
flags.addFlags("-DANDROID")
111+
.addFlags("-I" + new File(Environment.getSdCardHomeDir(), "SDL/include").getAbsolutePath())
112+
//ld flags
113+
.addFlags("-shared")
114+
.addFlags(new File(Environment.getSdCardHomeDir(), "SDL/lib/SDL_android_main.o").getAbsolutePath())
115+
.addFlags("-I" + new File(Environment.getSdCardHomeDir(), "SDL/lib").getAbsolutePath())
116+
.addFlags("-lSDL2")
117+
.addFlags("-llog")
118+
.addFlags("-lm")
119+
.addFlags("-o", mOutFile.getAbsolutePath());
120+
121+
return flags.toList();
122+
}
123+
124+
protected ArrayList<String> buildExecutableFlags(File[] sourceFiles) {
125+
File source = sourceFiles[0];
126+
String nameNoExt = source.getName().substring(0, source.getName().lastIndexOf("."));
127+
mOutFile = new File(source.getParent(), nameNoExt);
128+
129+
CommandBuilder args = new CommandBuilder();
130+
args.addFlags("-o", mOutFile.getAbsolutePath());
131+
return args.toList();
132+
}
133+
134+
protected ArrayList<String> buildNativeActivityFlags(File[] sourceFiles) {
135+
File source = sourceFiles[0];
136+
String nameNoExt = source.getName().substring(0, source.getName().lastIndexOf("."));
137+
138+
if (DLog.DEBUG) DLog.d(TAG, "buildArgs: build native activity");
139+
String soName = "lib" + nameNoExt + ".so";
140+
mOutFile = new File(source.getParent(), soName);
141+
String cctools = Environment.getCCtoolsDir(mContext);
142+
CommandBuilder args = new CommandBuilder();
143+
args.addFlags("-I" + cctools + "/sources/native_app_glue/")
144+
.addFlags(cctools + "/sources/native_app_glue/android_native_app_glue.c")
145+
.addFlags("-Wl,-soname," + soName)
146+
.addFlags("-shared")
147+
.addFlags("-Wl,--no-undefined")
148+
.addFlags("-Wl,-z,noexecstack")
149+
.addFlags("-llog") //lib log
150+
.addFlags("-landroid") //android
151+
.addFlags("-lm")
152+
.addFlags("-o", mOutFile.getAbsolutePath());
153+
154+
return args.toList();
155+
}
156+
157+
113158
}

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.duy.ccppcompiler.compiler.ICompileSetting;
2222
import com.duy.ccppcompiler.compiler.shell.CommandBuilder;
2323
import com.duy.ccppcompiler.compiler.shell.CompileResult;
24-
import com.duy.ccppcompiler.packagemanager.Environment;
2524

2625
import java.io.File;
2726

@@ -32,8 +31,8 @@
3231
public class GPlusPlusCompiler extends GCCCompiler {
3332
private static final String G_PLUS_PLUS_COMPILER_NAME = "g++-4.9";
3433

35-
public GPlusPlusCompiler(Context context, boolean nativeActivity, ICompileSetting setting) {
36-
super(context, nativeActivity, setting);
34+
public GPlusPlusCompiler(Context context, boolean nativeActivity, boolean buildSDL, ICompileSetting setting) {
35+
super(context, nativeActivity, buildSDL, setting);
3736
}
3837

3938
@Override
@@ -48,32 +47,20 @@ public CompileResult compile(File[] sourceFiles) {
4847

4948
@Override
5049
protected String buildArgs(File[] sourceFiles) {
51-
File source = sourceFiles[0];
52-
String nameNoExt = source.getName().substring(0, source.getName().lastIndexOf("."));
53-
5450
CommandBuilder args = new CommandBuilder();
5551
for (File sourceFile : sourceFiles) {
5652
args.addFlags(sourceFile.getAbsolutePath());
5753
}
5854
if (mBuildNativeActivity) {
59-
mOutFile = new File(source.getParent(), "lib" + nameNoExt + ".so");
60-
String cCtoolsDir = Environment.getCCtoolsDir(mContext);
61-
args.addFlags(String.format("-I%s/sources/native_app_glue", cCtoolsDir))
62-
.addFlags(String.format("%s/sources/native_app_glue/android_native_app_glue.c", cCtoolsDir))
63-
.addFlags(String.format("-Wl,-soname,%s", mOutFile.getAbsolutePath()))
64-
.addFlags("-shared")
65-
.addFlags("-Wl,--no-undefined -Wl,-z,noexecstack")
66-
.addFlags("-llog")
67-
.addFlags("-landroid")
68-
.addFlags("-lm")
69-
.addFlags("-o", mOutFile.getAbsolutePath());
55+
args.addFlags(super.buildNativeActivityFlags(sourceFiles));
56+
} else if (mBuildSDL) {
57+
args.addFlags(super.buildNativeActivityFlags(sourceFiles));
7058
} else {
71-
mOutFile = new File(source.getParent(), nameNoExt);
72-
args.addFlags("-o", mOutFile.getAbsolutePath());
59+
args.addFlags(super.buildExecutableFlags(sourceFiles));
7360
}
7461

7562
if (mSetting != null) {
76-
args.addFlags(mSetting.getCFlags());
63+
args.addFlags(mSetting.getCxxFlags());
7764
args.addFlags(mSetting.getLinkerFlags());
7865
}
7966

app/src/main/java/com/duy/ccppcompiler/compiler/manager/CompileManager.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717

1818
package com.duy.ccppcompiler.compiler.manager;
1919

20-
import android.app.NativeActivity;
2120
import android.content.Intent;
22-
import android.widget.Toast;
2321

2422
import com.duy.ccppcompiler.compiler.shell.CommandResult;
2523
import com.duy.ccppcompiler.compiler.shell.CompileResult;
26-
import com.duy.ccppcompiler.compiler.shell.NativeActivityCompileResult;
2724
import com.duy.ccppcompiler.console.TermActivity;
2825
import com.duy.ccppcompiler.packagemanager.Environment;
2926
import com.duy.editor.CodeEditorActivity;
@@ -50,33 +47,20 @@ public CompileManager(CodeEditorActivity activity) {
5047
@Override
5148
public void onCompileSuccess(CommandResult result) {
5249
super.onCompileSuccess(result);
53-
File internalBinary = null;
50+
5451
if (result instanceof CompileResult) {
55-
File binFile = ((CompileResult) result).getBinaryFile();
56-
if (binFile == null) {
52+
File binaryFile = ((CompileResult) result).getBinaryFile();
53+
if (binaryFile == null){
5754
return;
5855
}
59-
60-
//copy to internal storage
56+
File internalBinary;
6157
try {
62-
internalBinary = copyToTmpExeDirAndSetExecutable(binFile);
63-
64-
} catch (Exception e) {
58+
internalBinary = copyToTmpExeDirAndSetExecutable(binaryFile);
59+
} catch (IOException e) {
60+
handleInternalException(e);
6561
e.printStackTrace();
66-
Toast.makeText(getActivity(), String.format("Internal error: %s. Please report bug on GitHub",
67-
e.getMessage()), Toast.LENGTH_SHORT).show();
6862
return;
6963
}
70-
}
71-
72-
if (result instanceof NativeActivityCompileResult) {
73-
//now run binary file
74-
Intent intent = new Intent(getActivity(), NativeActivity.class);
75-
//from jni: main.cpp
76-
intent.putExtra("nativeApp", internalBinary.getAbsolutePath());
77-
getActivity().startActivity(intent);
78-
79-
} else if (result instanceof CompileResult) {
8064

8165
Intent intent = new Intent(getActivity(), TermActivity.class);
8266
intent.putExtra(BuildConstants.EXTRA_FILE_NAME, internalBinary.getAbsolutePath());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void onCompileSuccess(CommandResult commandResult) {
105105
}
106106

107107
@MainThread
108-
protected void handleException(Throwable e) {
108+
protected void handleInternalException(Throwable e) {
109109
if (e == null) {
110110
return;
111111
}

app/src/main/java/com/duy/ccppcompiler/compiler/manager/NativeActivityCompileManager.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void onCompileSuccess(CommandResult result) {
4343
NativeActivityCompileResult compileResult = (NativeActivityCompileResult) result;
4444
internalBinary = copyToTmpExeDirAndSetExecutable(compileResult.getBinaryFile());
4545
} catch (IOException e) {
46-
handleException(e);
46+
handleInternalException(e);
4747
return;
4848
}
4949

@@ -52,8 +52,6 @@ public void onCompileSuccess(CommandResult result) {
5252
//from jni: main.cpp
5353
intent.putExtra("nativeApp", internalBinary.getAbsolutePath());
5454
getActivity().startActivity(intent);
55-
} else {
56-
super.onCompileSuccess(result);
5755
}
5856
}
5957
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,7 @@ public String build() {
7373
}
7474

7575

76+
public ArrayList<String> toList() {
77+
return flags;
78+
}
7679
}

app/src/main/java/com/duy/ccppcompiler/packagemanager/Environment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,5 @@ public static String getSdCardExampleDir() {
306306
File file = new File(getSdCardHomeDir(), "Examples");
307307
return mkdirIfNotExist(file);
308308
}
309+
309310
}

0 commit comments

Comments
 (0)