Skip to content

Commit b697648

Browse files
committed
Improve build native activity
1 parent 97073ef commit b697648

File tree

13 files changed

+295
-150
lines changed

13 files changed

+295
-150
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.support.annotation.Nullable;
2222

2323
import com.duy.ccppcompiler.compiler.compilers.ICompiler;
24+
import com.duy.ccppcompiler.compiler.manager.ICompileManager;
2425
import com.duy.ccppcompiler.compiler.shell.CommandResult;
2526

2627
import java.io.File;

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ protected String buildArgs(File[] sourceFiles) {
7878
for (File sourceFile : sourceFiles) {
7979
args.addFlags(sourceFile.getAbsolutePath());
8080
}
81-
if (mSetting != null) {
82-
args.addFlags(mSetting.getCFlags());
83-
}
8481

8582
if (mBuildNativeActivity) {
8683
if (DLog.DEBUG) DLog.d(TAG, "buildArgs: build native activity");
@@ -105,9 +102,10 @@ protected String buildArgs(File[] sourceFiles) {
105102
args.addFlags("-o", mOutFile.getAbsolutePath());
106103
}
107104

108-
// if (Build.VERSION.SDK_INT >= 21) {
109-
// args.addFlags("-pie");
110-
// }
105+
if (mSetting != null) {
106+
args.addFlags(mSetting.getCFlags());
107+
args.addFlags(mSetting.getLinkerFlags());
108+
}
111109

112110
return args.build();
113111
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
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;
2322
import com.duy.ccppcompiler.compiler.shell.CommandBuilder;
@@ -56,10 +55,6 @@ protected String buildArgs(File[] sourceFiles) {
5655
for (File sourceFile : sourceFiles) {
5756
args.addFlags(sourceFile.getAbsolutePath());
5857
}
59-
if (mSetting != null) {
60-
args.addFlags(mSetting.getCxxFlags());
61-
}
62-
6358
if (mBuildNativeActivity) {
6459
mOutFile = new File(source.getParent(), "lib" + nameNoExt + ".so");
6560
String cCtoolsDir = Environment.getCCtoolsDir(mContext);
@@ -77,8 +72,9 @@ protected String buildArgs(File[] sourceFiles) {
7772
args.addFlags("-o", mOutFile.getAbsolutePath());
7873
}
7974

80-
if (Build.VERSION.SDK_INT >= 21) {
81-
args.addFlags("-pie");
75+
if (mSetting != null) {
76+
args.addFlags(mSetting.getCFlags());
77+
args.addFlags(mSetting.getLinkerFlags());
8278
}
8379

8480
return args.build();

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

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
/*
2-
* Copyright 2018 Mr Duy
2+
* Copyright (C) 2018 Duy Tran Le
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
78
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
913
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1516
*/
1617

17-
package com.duy.ccppcompiler.compiler;
18+
package com.duy.ccppcompiler.compiler.manager;
1819

1920
import android.app.NativeActivity;
2021
import android.content.Intent;
@@ -34,6 +35,7 @@
3435
import java.io.File;
3536
import java.io.FileInputStream;
3637
import java.io.FileOutputStream;
38+
import java.io.IOException;
3739

3840
/**
3941
* Created by Duy on 25-Apr-18.
@@ -57,15 +59,8 @@ public void onCompileSuccess(CommandResult result) {
5759

5860
//copy to internal storage
5961
try {
60-
internalBinary = new File(Environment.getTmpExeDir(getActivity()), binFile.getName());
61-
FileInputStream inputStream = new FileInputStream(binFile);
62-
FileOutputStream outputStream = new FileOutputStream(internalBinary);
63-
IOUtils.copy(inputStream, outputStream);
64-
inputStream.close();
65-
outputStream.close();
66-
67-
//set executable
68-
Utils.chmod(internalBinary.getAbsolutePath(), 0x1ed/*0775*/);
62+
internalBinary = copyToTmpExeDirAndSetExecutable(binFile);
63+
6964
} catch (Exception e) {
7065
e.printStackTrace();
7166
Toast.makeText(getActivity(), String.format("Internal error: %s. Please report bug on GitHub",
@@ -90,4 +85,18 @@ public void onCompileSuccess(CommandResult result) {
9085
}
9186
}
9287

88+
protected File copyToTmpExeDirAndSetExecutable(File binFile) throws IOException {
89+
File internalBinary = new File(Environment.getTmpExeDir(getActivity()), binFile.getName());
90+
FileInputStream inputStream = new FileInputStream(binFile);
91+
FileOutputStream outputStream = new FileOutputStream(internalBinary);
92+
IOUtils.copy(inputStream, outputStream);
93+
inputStream.close();
94+
outputStream.close();
95+
96+
//set executable
97+
Utils.chmod(internalBinary.getAbsolutePath(), 0x1ed/*0775*/);
98+
99+
return internalBinary;
100+
}
101+
93102
}

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
/*
2-
* Copyright 2018 Mr Duy
2+
* Copyright (C) 2018 Duy Tran Le
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
78
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
913
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1516
*/
1617

17-
package com.duy.ccppcompiler.compiler;
18+
package com.duy.ccppcompiler.compiler.manager;
1819

1920
import android.app.ProgressDialog;
2021
import android.support.annotation.MainThread;
@@ -23,6 +24,7 @@
2324
import android.widget.Toast;
2425

2526
import com.duy.ccppcompiler.R;
27+
import com.duy.ccppcompiler.compiler.CompileTask;
2628
import com.duy.ccppcompiler.compiler.compilers.ICompiler;
2729
import com.duy.ccppcompiler.compiler.shell.CommandResult;
2830
import com.duy.ccppcompiler.compiler.shell.OutputParser;
@@ -32,6 +34,7 @@
3234
import com.duy.ide.DiagnosticPresenter;
3335
import com.duy.ide.DiagnosticsCollector;
3436
import com.duy.ide.editor.SimpleEditorActivity;
37+
import com.jecelyin.common.utils.UIUtils;
3538
import com.jecelyin.editor.v2.widget.menu.MenuDef;
3639

3740
import java.io.File;
@@ -101,6 +104,14 @@ public void onCompileSuccess(CommandResult commandResult) {
101104
finishCompile(commandResult);
102105
}
103106

107+
@MainThread
108+
protected void handleException(Throwable e) {
109+
if (e == null) {
110+
return;
111+
}
112+
UIUtils.alert(mActivity, e.getMessage());
113+
}
114+
104115
private void finishCompile(CommandResult compileResult) {
105116
hideDialog();
106117

app/src/main/java/com/duy/ccppcompiler/compiler/ICompileManager.java renamed to app/src/main/java/com/duy/ccppcompiler/compiler/manager/ICompileManager.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
/*
2-
* Copyright 2018 Mr Duy
2+
* Copyright (C) 2018 Duy Tran Le
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
78
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
913
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1516
*/
1617

17-
package com.duy.ccppcompiler.compiler;
18+
package com.duy.ccppcompiler.compiler.manager;
1819

1920
import android.support.annotation.MainThread;
2021

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (C) 2018 Duy Tran Le
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.duy.ccppcompiler.compiler.manager;
19+
20+
import android.app.NativeActivity;
21+
import android.content.Intent;
22+
23+
import com.duy.ccppcompiler.compiler.shell.CommandResult;
24+
import com.duy.ccppcompiler.compiler.shell.NativeActivityCompileResult;
25+
import com.duy.editor.CodeEditorActivity;
26+
27+
import java.io.File;
28+
import java.io.IOException;
29+
30+
public class NativeActivityCompileManager extends CompileManager {
31+
32+
public NativeActivityCompileManager(CodeEditorActivity activity) {
33+
super(activity);
34+
}
35+
36+
@Override
37+
public void onCompileSuccess(CommandResult result) {
38+
super.onCompileSuccess(result);
39+
40+
if (result instanceof NativeActivityCompileResult) {
41+
File internalBinary;
42+
try {
43+
NativeActivityCompileResult compileResult = (NativeActivityCompileResult) result;
44+
internalBinary = copyToTmpExeDirAndSetExecutable(compileResult.getBinaryFile());
45+
} catch (IOException e) {
46+
handleException(e);
47+
return;
48+
}
49+
50+
//now run binary file
51+
Intent intent = new Intent(getActivity(), NativeActivity.class);
52+
//from jni: main.cpp
53+
intent.putExtra("nativeApp", internalBinary.getAbsolutePath());
54+
getActivity().startActivity(intent);
55+
} else {
56+
super.onCompileSuccess(result);
57+
}
58+
}
59+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2018 Duy Tran Le
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.duy.ccppcompiler.compiler.manager;
19+
20+
import android.content.Intent;
21+
22+
import com.duy.ccppcompiler.compiler.shell.CommandResult;
23+
import com.duy.ccppcompiler.compiler.shell.CompileResult;
24+
import com.duy.editor.CodeEditorActivity;
25+
import com.jecelyin.common.utils.UIUtils;
26+
27+
import java.io.File;
28+
29+
public class SDLCompileManager extends CompileManager {
30+
31+
public SDLCompileManager(CodeEditorActivity activity) {
32+
super(activity);
33+
}
34+
35+
@Override
36+
public void onCompileSuccess(CommandResult result) {
37+
if (result instanceof CompileResult) {
38+
File binaryFile = ((CompileResult) result).getBinaryFile();
39+
if (binaryFile == null) {
40+
super.onCompileSuccess(result);
41+
return;
42+
}
43+
44+
Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage("com.duy.c.cpp.compiler.sdlplugin");
45+
if (intent != null) {
46+
intent.putExtra("sdlmain", binaryFile.getAbsolutePath());
47+
getActivity().startActivity(intent);//null pointer check in case package name was not found
48+
} else {
49+
UIUtils.alert(getActivity(), "Can not run SDL application");
50+
}
51+
} else {
52+
super.onCompileSuccess(result);
53+
}
54+
}
55+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
package com.duy.ccppcompiler.compiler.shell;
22

3+
import android.support.annotation.Nullable;
4+
5+
import java.io.File;
6+
37
/**
48
* Compile result when build native activity
59
*/
610
public class NativeActivityCompileResult extends CompileResult {
11+
@Nullable
12+
private File binaryFile;
713

814
public NativeActivityCompileResult(CommandResult commandResult) {
915
super(commandResult);
1016
}
1117

18+
@Nullable
19+
public File getBinaryFile() {
20+
return binaryFile;
21+
}
22+
23+
public void setBinaryFile(@Nullable File binaryFile) {
24+
this.binaryFile = binaryFile;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "CompileResult{" +
30+
"binaryFile=" + binaryFile +
31+
"} " + super.toString();
32+
}
1233
}

0 commit comments

Comments
 (0)