Skip to content

Commit 22287b9

Browse files
committed
Improve auto detect include and ld flags
1 parent 5bc88d0 commit 22287b9

File tree

4 files changed

+79
-51
lines changed

4 files changed

+79
-51
lines changed

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

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,67 @@
1414

1515
public class LinkerFlagsDetector {
1616
public static final LinkerFlagsDetector INSTANCE = new LinkerFlagsDetector();
17-
private static final Pattern FILE_NAME = Pattern.compile("[A-Za-z_.][A-Za-z0-9_.]+");
18-
//-lSDL2
19-
private static final Pattern INCLUDE_SDL = Pattern.compile("#include\\s+[<\"]SDL.h[>\"]");
17+
private static final Pattern FILE_NAME = Pattern.compile("[A-Za-z_.][A-Za-z0-9_.\\-]+");
18+
19+
2020
//-lGLESv1_CM
21-
private static final Pattern INCLUDE_GLESV1_CM
22-
= Pattern.compile("#include\\s+[<\"]GLES/(" + FILE_NAME.pattern() + ")[>\"]");
21+
private static final Pattern INCLUDE_GLES_V1_CM
22+
= Pattern.compile("#include\\s+[<\"]" +
23+
"(GLES/gl\\.h|GLES/glext\\.h|GLES/glplatform\\.h|((\\S+)?gl[^2]+\\.h))" +
24+
"[>\"]");
25+
//case: gl2.h, gl2ext.h, gl2platform.h, SDL_opengles2.h, ..
26+
private static final Pattern INCLUDE_GLES_V2
27+
= Pattern.compile("#include\\s+[<\"]" +
28+
"(GLES2/gl2\\.h|GLES2/gl2ext\\.h|GLES2/gl2platform\\.h|((\\S+)?gl(\\S+)?2(\\S+)?\\.h))" +
29+
"[>\"]");
30+
//-lOpenSLES
31+
private static final Pattern INCLUDE_OPEN_SLES
32+
= Pattern.compile("#include\\s+[<\"]SLES/(" + FILE_NAME.pattern() + ")[>\"]");
2333

2434
//-landroid
2535
private static final Pattern INCLUDE_ANDROID
2636
= Pattern.compile("#include\\s+[<\"]android/(" + FILE_NAME.pattern() + ")[>\"]");
2737
//-llog
2838
private static final Pattern INCLUDE_ANDROID_LOG
29-
= Pattern.compile("#include\\s+[<\"]<android/log.h>[>\"]");
39+
= Pattern.compile("#include\\s+[<\"]<android/log\\.h>[>\"]");
3040
//-lEGL
3141
private static final Pattern INCLUDE_EGL
3242
= Pattern.compile("#include\\s+[<\"]EGL/(" + FILE_NAME.pattern() + ")[>\"]");
3343
//-latomic
34-
private static final Pattern INCLUDE_ATOMIC = Pattern.compile("#include\\s+[<\"]atomic.h[>\"]");
44+
private static final Pattern INCLUDE_ATOMIC = Pattern.compile("#include\\s+[<\"]atomic\\.h[>\"]");
3545
//-latomic
36-
private static final Pattern INCLUDE_MATH = Pattern.compile("#include\\s+[<\"]math.h[>\"]");
46+
private static final Pattern INCLUDE_MATH = Pattern.compile("#include\\s+[<\"]math\\.h[>\"]");
47+
private static final Pattern INCLUDE_ZLIB = Pattern.compile("#include\\s+[<\"]zlib\\.h[>\"]");
3748

49+
//-lSDL2
50+
private static final Pattern INCLUDE_SDL = Pattern.compile("#include\\s+[<\"]SDL\\.h[>\"]");
51+
//-lSDL2_ttf
52+
private static final Pattern INCLUDE_SDL_TTF = Pattern.compile("#include\\s+[<\"]SDL_ttf\\.h[>\"]");
53+
//-lSDL2_image
54+
private static final Pattern INCLUDE_SDL_IMAGE = Pattern.compile("#include\\s+[<\"]SDL_image\\.h[>\"]");
55+
private static final Pattern INCLUDE_SDL_MIXER = Pattern.compile("#include\\s+[<\"]SDL_mixer\\.h[>\"]");
56+
private static final Pattern INCLUDE_SDL_NET = Pattern.compile("#include\\s+[<\"]SDL_net\\.h[>\"]");
3857

3958
private static final ArrayList<Pair<Pattern, String>> PATTERNS;
4059

4160
static {
4261
PATTERNS = new ArrayList<>();
43-
PATTERNS.add(new Pair<>(INCLUDE_SDL, "-lSDL2"));
4462
PATTERNS.add(new Pair<>(INCLUDE_ANDROID, "-landroid"));
4563
PATTERNS.add(new Pair<>(INCLUDE_ANDROID_LOG, "-llog"));
4664
PATTERNS.add(new Pair<>(INCLUDE_EGL, "-lEGL"));
4765
PATTERNS.add(new Pair<>(INCLUDE_MATH, "-lm"));
48-
PATTERNS.add(new Pair<>(INCLUDE_GLESV1_CM, "-lGLESv1_CM"));
66+
PATTERNS.add(new Pair<>(INCLUDE_GLES_V1_CM, "-lGLESv1_CM"));
4967
PATTERNS.add(new Pair<>(INCLUDE_ATOMIC, "-latomic"));
68+
PATTERNS.add(new Pair<>(INCLUDE_GLES_V1_CM, "-lGLESv1_CM"));
69+
PATTERNS.add(new Pair<>(INCLUDE_GLES_V2, "-lGLESv2"));
70+
PATTERNS.add(new Pair<>(INCLUDE_OPEN_SLES, "-lOpenSLES"));
71+
PATTERNS.add(new Pair<>(INCLUDE_ZLIB, "-lz"));
72+
73+
PATTERNS.add(new Pair<>(INCLUDE_SDL, "-lSDL2"));
74+
PATTERNS.add(new Pair<>(INCLUDE_SDL_TTF, "-lSDL2_ttf"));
75+
PATTERNS.add(new Pair<>(INCLUDE_SDL_IMAGE, "-lSDL2_image"));
76+
PATTERNS.add(new Pair<>(INCLUDE_SDL_MIXER, "-lSDL2_mixer"));
77+
PATTERNS.add(new Pair<>(INCLUDE_SDL_NET, "-lSDL2_net"));
5078

5179
}
5280

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public CommandResult compile(File[] sourceFiles, ILogger logger) {
7474

7575
final String cmd = argumentBuilder.build();
7676
String debugStr = cmd.replaceAll("\\s+", "\n");
77-
if (DLog.DEBUG) DLog.d(TAG, "debugStr = \n" + debugStr);
77+
if (DLog.DEBUG) DLog.w(TAG, "debugStr = \n" + debugStr);
7878
if (logger != null) {
7979
logger.verbose("Compiler argument: " + cmd);
8080
}
@@ -169,9 +169,6 @@ private void buildNativeActivityFlags(ArgumentBuilder args, File[] sourceFiles)
169169
.addFlags("-shared")
170170
.addFlags("-Wl,--no-undefined")
171171
.addFlags("-Wl,-z,noexecstack")
172-
// .addFlags("-llog") //lib log
173-
// .addFlags("-landroid") //android
174-
// .addFlags("-lm")
175172
.addFlags("-o", outputScope.getBinaryFile().getAbsolutePath());
176173
}
177174

@@ -189,8 +186,7 @@ private void buildSDLActivity(GccArgumentBuilder args, File[] sourceFiles) {
189186
.addFlags("-I" + sdCardHomeDir + "/SDL/include")
190187
.addFlags("-shared")
191188
.addFlags(sdCardHomeDir + "/SDL/lib/SDL_android_main.o")
192-
.addFlags("-L" + sdCardHomeDir + "SDL/lib")
193-
.addFlags("-lSDL2")
189+
.addFlags("-L" + sdCardHomeDir + "/SDL/lib")
194190
.addFlags("-o", outputScope.getBinaryFile().getAbsolutePath());
195191
}
196192

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

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package com.duy.ccppcompiler.compiler.shell;
22

33
import android.support.annotation.NonNull;
4-
import android.support.v4.util.Pair;
4+
import android.support.v4.util.ArraySet;
55

66
import java.util.ArrayList;
77
import java.util.Collection;
8-
import java.util.Collections;
9-
import java.util.Comparator;
8+
import java.util.HashMap;
109

1110
public class GccArgumentBuilder extends ArgumentBuilder {
1211
private String program;
13-
private ArrayList<Pair<Type, String>> mFlags = new ArrayList<>();
12+
private HashMap<Type, ArrayList<String>> mFlags = new HashMap<>();
1413

1514
public GccArgumentBuilder(String program) {
1615
this.program = program;
@@ -32,8 +31,12 @@ public GccArgumentBuilder addFlag(String flag) {
3231
return this;
3332
}
3433

35-
public void addFlag(Type type, String name) {
36-
mFlags.add(new Pair<>(type, name));
34+
public GccArgumentBuilder addFlag(Type type, String name) {
35+
if (mFlags.get(type) == null) {
36+
mFlags.put(type, new ArrayList<String>());
37+
}
38+
mFlags.get(type).add(name);
39+
return this;
3740
}
3841

3942
public GccArgumentBuilder addFlags(Collection<String> flags) {
@@ -42,38 +45,28 @@ public GccArgumentBuilder addFlags(Collection<String> flags) {
4245
}
4346

4447

45-
@SuppressWarnings("ConstantConditions")
4648
@NonNull
4749
public String build() {
4850
StringBuilder cmd = new StringBuilder();
4951
cmd.append(program);
5052
if (mFlags.size() == 0) {
5153
return cmd.toString();
5254
}
53-
54-
Collections.sort(mFlags, new Comparator<Pair<Type, String>>() {
55-
@SuppressWarnings("ConstantConditions")
56-
@Override
57-
public int compare(Pair<Type, String> o1, Pair<Type, String> o2) {
58-
return -o1.first.getPriority().compareTo(o2.first.getPriority());
55+
for (Type type : Type.values()) {
56+
Collection<String> flags = mFlags.get(type);
57+
if (flags == null) {
58+
continue;
5959
}
60-
});
61-
Pair<Type, String> prev = null;
62-
for (int i = 0; i < mFlags.size(); i++) {
63-
final Pair<Type, String> flag = mFlags.get(i);
64-
final Type type = flag.first;
65-
final String name = flag.second;
66-
if (name != null && !name.isEmpty()) {
67-
if (prev != null) {
68-
if (prev.equals(flag) && !type.isAcceptDuplicate()) {
69-
continue;
60+
if (!type.isAcceptDuplicate()) {
61+
flags = new ArraySet<>(flags);
62+
}
63+
for (String flag : flags) {
64+
if (flag != null && !flag.isEmpty()) {
65+
if (cmd.length() != 0) {
66+
cmd.append(" ");
7067
}
68+
cmd.append(flag);
7169
}
72-
if (cmd.length() != 0) {
73-
cmd.append(" ");
74-
}
75-
cmd.append(name);
76-
prev = flag;
7770
}
7871
}
7972
return cmd.toString();
@@ -82,7 +75,7 @@ public int compare(Pair<Type, String> o1, Pair<Type, String> o2) {
8275

8376
public enum Type {
8477
UNSPECIFIED(4, true),
85-
CPP_FLAG(3, true),
78+
CXX_FLAG(3, true),
8679
C_FLAG(2, true),
8780
LD_FLAG(1, false);
8881

editor.txt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
#include <stdio.h>
2-
#include <float.h>
1+
#include <ncurses.h> /* ncurses.h includes stdio.h */
2+
#include <string.h>
33

4-
int main () {
5-
printf("The maximum value of float = %.10e", FLT_MAX);
6-
printf("The minimum value of float = %.10e", FLT_MIN);
4+
int main()
5+
{
6+
char mesg[]="Just a string"; /* message to be appeared on the screen */
7+
int row,col; /* to store the number of rows and *
8+
* the number of colums of the screen */
9+
initscr(); /* start the curses mode */
10+
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
11+
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
12+
/* print the message at the center of the screen */
13+
mvprintw(row-2,0,"This screen has %d rows and %d columns",row,col);
14+
printw("Try resizing your window(if possible) and then run this program again");
15+
refresh();
16+
getch();
17+
endwin();
718

8-
printf("The number of digits in the number = %.10e", FLT_MANT_DIG);
19+
return 0;
920
}

0 commit comments

Comments
 (0)