FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ALLVM/build.cpp at main · CKAndroidProject/ALLVM · GitHub
CKAndroidProject
/
ALLVM
Public
forked from
abcdefgjh-li/ALLVM
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
ALLVM
/
build.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1627 lines (1419 loc) · 58.4 KB
Breadcrumbs
ALLVM
/
build.cpp
Copy path
File metadata and controls
1627 lines (1419 loc) · 58.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
include
<
windows.h
>
#
include
<
cstdio
>
#
include
<
cstdlib
>
#
include
<
cstring
>
#
include
<
string
>
#
include
<
vector
>
#
include
<
fstream
>
#
include
<
sstream
>
#
include
<
algorithm
>
#
include
<
cctype
>
static
std::string g_script_dir;
static
std::string g_build_dir;
static
std::string g_ndk_dir;
static
std::string g_ndk_bin;
static
std::string g_acode_dir;
static
std::string g_apk_output_dir;
static
std::string g_zstd_dir;
static
std::string g_zlib_dir;
struct
TestRunOptions
{
bool
custom_test =
false
;
std::string project_dir;
std::string app_build_script =
"
jni/Android.mk
"
;
std::string app_application_mk =
"
jni/Application.mk
"
;
std::string binary_name;
std::string local_binary_path;
std::string device_path;
std::string run_command;
std::string serial;
std::string abi;
int
timeout_sec =
20
;
bool
skip_ndk_build =
false
;
bool
clean_test =
false
;
};
static
bool
file_exists
(
const
std::string& path) {
return
GetFileAttributesA
(path.
c_str
()) !=
INVALID_FILE_ATTRIBUTES
;
}
static
bool
get_file_time
(
const
std::string& path,
FILETIME
& time) {
WIN32_FILE_ATTRIBUTE_DATA
data;
if
(!
GetFileAttributesExA
(path.
c_str
(), GetFileExInfoStandard, &data))
return
false
;
time = data.
ftLastWriteTime
;
return
true
;
}
static
bool
is_file_newer_or_equal
(
const
std::string& output,
const
std::string& input) {
FILETIME
output_time, input_time;
if
(!
get_file_time
(output, output_time))
return
false
;
if
(!
get_file_time
(input, input_time))
return
true
;
return
CompareFileTime
(&output_time, &input_time) >=
0
;
}
static
bool
dir_exists
(
const
std::string& path) {
DWORD
attr =
GetFileAttributesA
(path.
c_str
());
return
(attr !=
INVALID_FILE_ATTRIBUTES
) && (attr &
FILE_ATTRIBUTE_DIRECTORY
);
}
static
std::string
get_full_path_copy
(
const
std::string& path) {
char
buf[
MAX_PATH
];
DWORD
len =
GetFullPathNameA
(path.
c_str
(),
MAX_PATH
, buf,
NULL
);
if
(len ==
0
|| len >=
MAX_PATH
)
return
path;
return
std::string
(buf, len);
}
static
std::string
path_basename
(
const
std::string& path) {
size_t
pos = path.
find_last_of
(
"
\\
/
"
);
return
(pos == std::string::npos) ? path : path.
substr
(pos +
1
);
}
static
void
init_paths
() {
char
buf[
MAX_PATH
];
GetModuleFileNameA
(
NULL
, buf,
MAX_PATH
);
std::string
exe_path
(buf);
size_t
pos = exe_path.
find_last_of
(
"
\\
/
"
);
g_script_dir = exe_path.
substr
(
0
, pos);
g_build_dir = g_script_dir +
"
\\
build-windows
"
;
g_acode_dir = g_script_dir +
"
\\
apkUI
"
;
g_apk_output_dir = g_build_dir +
"
\\
bin
"
;
g_zstd_dir = g_script_dir +
"
\\
zstd
"
;
g_zlib_dir = g_script_dir +
"
\\
zlib
"
;
//
Search NDK in current directory first
std::string local_ndk = g_script_dir +
"
\\
android-ndk-r30-beta1-windows
"
;
if
(
dir_exists
(local_ndk)) {
g_ndk_dir = local_ndk;
}
else
{
//
Then search in parent directory
std::string p_ndk = g_script_dir +
"
\\
..
\\
android-ndk-r30-beta1-windows
"
;
char
abs_path[
MAX_PATH
];
if
(
GetFullPathNameA
(p_ndk.
c_str
(),
MAX_PATH
, abs_path,
NULL
)) {
if
(
dir_exists
(abs_path)) {
g_ndk_dir = abs_path;
}
}
}
if
(!g_ndk_dir.
empty
()) {
g_ndk_bin = g_ndk_dir +
"
\\
toolchains
\\
llvm
\\
prebuilt
\\
windows-x86_64
\\
bin
"
;
}
}
//
---- debug instrumentation (aarch64-ndk-crash)
static
void
dbg_log
(
const
std::string& event,
const
std::string& detail) {
static
const
std::string session =
"
aarch64-ndk-crash
"
;
std::string log_path = g_script_dir +
"
\\
trae-debug-log-
"
+ session +
"
.ndjson
"
;
SYSTEMTIME
st;
GetSystemTime
(&st);
char
ts[
64
];
sprintf
(ts,
"
%04d-%02d-%02dT%02d:%02d:%02dZ
"
,
(
int
)st.
wYear
, (
int
)st.
wMonth
, (
int
)st.
wDay
,
(
int
)st.
wHour
, (
int
)st.
wMinute
, (
int
)st.
wSecond
);
std::ofstream
ofs
(log_path, std::ios::app);
if
(!ofs.
is_open
())
return
;
std::string esc;
esc.
reserve
(detail.
size
() +
16
);
for
(
char
c : detail) {
if
(c ==
'
\\
'
) esc +=
"
\\\\
"
;
else
if
(c ==
'
\"
'
) esc +=
"
\\\"
"
;
else
if
(c ==
'
\n
'
) esc +=
"
\\
n
"
;
else
esc += c;
}
ofs <<
"
{
"
<<
"
\"
ts
\"
:
\"
"
<< ts <<
"
\"
,
"
<<
"
\"
session
\"
:
\"
"
<< session <<
"
\"
,
"
<<
"
\"
event
\"
:
\"
"
<< event <<
"
\"
,
"
<<
"
\"
detail
\"
:
\"
"
<< esc <<
"
\"
"
<<
"
}
"
<< std::endl;
}
static
std::string
find_vs
() {
const
char
* vcvars_path =
"
C:
\\
Program Files
\\
Microsoft Visual Studio
\\
2022
\\
Enterprise
\\
VC
\\
Auxiliary
\\
Build
\\
vcvars64.bat
"
;
if
(
file_exists
(vcvars_path))
return
vcvars_path;
return
"
"
;
}
static
void
dir_create
(
const
std::string& path) {
CreateDirectoryA
(path.
c_str
(),
NULL
);
}
static
std::string
format_win32_error
(
DWORD
err) {
if
(err ==
0
)
return
"
success
"
;
char
* buf =
nullptr
;
DWORD
len =
FormatMessageA
(
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
,
NULL
, err,
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
reinterpret_cast
<
LPSTR
>(&buf),
0
,
NULL
);
std::string msg = (len && buf) ?
std::string
(buf, len) : (
"
error
"
+
std::to_string
(err));
if
(buf)
LocalFree
(buf);
while
(!msg.
empty
() && (msg.
back
() ==
'
\r
'
|| msg.
back
() ==
'
\n
'
)) msg.
pop_back
();
return
msg;
}
static
bool
copy_file
(
const
std::string& src,
const
std::string& dst,
bool
overwrite =
true
) {
if
(!
file_exists
(src)) {
printf
(
"
[Error] copy source not found: %s
\n
"
, src.
c_str
());
return
false
;
}
if
(overwrite &&
file_exists
(dst)) {
DWORD
attr =
GetFileAttributesA
(dst.
c_str
());
if
(attr !=
INVALID_FILE_ATTRIBUTES
&& (attr &
FILE_ATTRIBUTE_READONLY
)) {
SetFileAttributesA
(dst.
c_str
(), attr & ~
FILE_ATTRIBUTE_READONLY
);
}
}
if
(
CopyFileA
(src.
c_str
(), dst.
c_str
(), overwrite ?
FALSE
:
TRUE
)) {
if
(
file_exists
(dst))
return
true
;
printf
(
"
[Error] copy reported success but destination missing: %s
\n
"
, dst.
c_str
());
return
false
;
}
DWORD
copy_err =
GetLastError
();
if
(overwrite &&
file_exists
(dst)) {
if
(
DeleteFileA
(dst.
c_str
()) &&
CopyFileA
(src.
c_str
(), dst.
c_str
(),
TRUE
)) {
if
(
file_exists
(dst))
return
true
;
printf
(
"
[Error] copy-after-delete reported success but destination missing: %s
\n
"
, dst.
c_str
());
return
false
;
}
copy_err =
GetLastError
();
}
printf
(
"
[Error] copy failed: %s -> %s (%s)
\n
"
,
src.
c_str
(), dst.
c_str
(),
format_win32_error
(copy_err).
c_str
());
return
false
;
}
static
std::string
trim_copy
(
const
std::string& input) {
size_t
begin =
0
;
while
(begin < input.
size
() &&
std::isspace
(
static_cast
<
unsigned
char
>(input[begin]))) begin++;
size_t
end = input.
size
();
while
(end > begin &&
std::isspace
(
static_cast
<
unsigned
char
>(input[end -
1
]))) end--;
return
input.
substr
(begin, end - begin);
}
static
std::string
get_env_var
(
const
char
* name) {
char
buf[
32767
];
DWORD
len =
GetEnvironmentVariableA
(name, buf,
sizeof
(buf));
if
(len ==
0
|| len >=
sizeof
(buf))
return
"
"
;
return
std::string
(buf, len);
}
static
std::string
search_path_binary
(
const
char
* name) {
char
buf[
MAX_PATH
];
DWORD
len =
SearchPathA
(
NULL
, name,
NULL
,
MAX_PATH
, buf,
NULL
);
if
(len ==
0
|| len >=
MAX_PATH
)
return
"
"
;
return
std::string
(buf, len);
}
static
std::string
find_compiler_cache_launcher
() {
std::string launcher =
search_path_binary
(
"
sccache.exe
"
);
if
(!launcher.
empty
())
return
launcher;
launcher =
search_path_binary
(
"
clcache.exe
"
);
return
launcher;
}
static
int
run_cmd
(
const
std::string& cmd,
const
std::string& cwd =
"
"
) {
std::string bat_file = cwd.
empty
() ?
"
build_tmp.bat
"
: cwd +
"
\\
build_tmp.bat
"
;
FILE
*f =
fopen
(bat_file.
c_str
(),
"
wb
"
);
if
(!f)
return
-
1
;
fprintf
(f,
"
@echo off
\r\n
chcp 65001 >nul
\r\n
"
);
if
(!cwd.
empty
())
fprintf
(f,
"
cd /d
\"
%s
\"\r\n
"
, cwd.
c_str
());
fprintf
(f,
"
%s
\r\n
"
, cmd.
c_str
());
fprintf
(f,
"
exit /b %%ERRORLEVEL%%
\r\n
"
);
fclose
(f);
int
ret =
system
(bat_file.
c_str
());
DeleteFileA
(bat_file.
c_str
());
return
ret;
}
static
int
run_cmd_vcvars
(
const
std::string& vcvars,
const
std::string& cmd,
const
std::string& cwd =
"
"
) {
std::string bat_file = cwd.
empty
() ?
"
build_tmp.bat
"
: cwd +
"
\\
build_tmp.bat
"
;
FILE
*f =
fopen
(bat_file.
c_str
(),
"
wb
"
);
if
(!f)
return
-
1
;
fprintf
(f,
"
@echo off
\r\n
chcp 65001 >nul
\r\n
"
);
fprintf
(f,
"
call
\"
%s
\"
>nul 2>&1
\r\n
"
, vcvars.
c_str
());
if
(!cwd.
empty
())
fprintf
(f,
"
cd /d
\"
%s
\"\r\n
"
, cwd.
c_str
());
fprintf
(f,
"
%s
\r\n
"
, cmd.
c_str
());
fprintf
(f,
"
exit /b %%ERRORLEVEL%%
\r\n
"
);
fclose
(f);
int
ret =
system
(bat_file.
c_str
());
DeleteFileA
(bat_file.
c_str
());
return
ret;
}
static
int
run_cmd_capture
(
const
std::string& cmd, std::string& output,
const
std::string& cwd =
"
"
) {
char
suffix[
32
];
sprintf
(suffix,
"
%llu
"
,
static_cast
<
unsigned
long
long
>(
GetTickCount64
()));
std::string base = cwd.
empty
() ? g_script_dir : cwd;
std::string bat_file = base +
"
\\
build_capture_
"
+ suffix +
"
.bat
"
;
std::string out_file = base +
"
\\
build_capture_
"
+ suffix +
"
.log
"
;
FILE
*f =
fopen
(bat_file.
c_str
(),
"
wb
"
);
if
(!f)
return
-
1
;
fprintf
(f,
"
@echo off
\r\n
chcp 65001 >nul
\r\n
"
);
if
(!cwd.
empty
())
fprintf
(f,
"
cd /d
\"
%s
\"\r\n
"
, cwd.
c_str
());
fprintf
(f,
"
%s >
\"
%s
\"
2>&1
\r\n
"
, cmd.
c_str
(), out_file.
c_str
());
fprintf
(f,
"
exit /b %%ERRORLEVEL%%
\r\n
"
);
fclose
(f);
int
ret =
system
(bat_file.
c_str
());
DeleteFileA
(bat_file.
c_str
());
std::ifstream
in
(out_file, std::ios::binary);
if
(in.
is_open
()) {
std::ostringstream ss;
ss << in.
rdbuf
();
output = ss.
str
();
in.
close
();
DeleteFileA
(out_file.
c_str
());
}
else
{
output.
clear
();
}
return
ret;
}
static
int
run_cmd_capture_timeout
(
const
std::string& cmd, std::string& output,
const
std::string& cwd,
DWORD
timeout_ms,
bool
* timed_out =
nullptr
) {
if
(timed_out) *timed_out =
false
;
char
suffix[
32
];
sprintf
(suffix,
"
%llu
"
,
static_cast
<
unsigned
long
long
>(
GetTickCount64
()));
std::string base = cwd.
empty
() ? g_script_dir : cwd;
std::string bat_file = base +
"
\\
build_capture_
"
+ suffix +
"
.bat
"
;
std::string out_file = base +
"
\\
build_capture_
"
+ suffix +
"
.log
"
;
FILE
*f =
fopen
(bat_file.
c_str
(),
"
wb
"
);
if
(!f)
return
-
1
;
fprintf
(f,
"
@echo off
\r\n
chcp 65001 >nul
\r\n
"
);
if
(!cwd.
empty
())
fprintf
(f,
"
cd /d
\"
%s
\"\r\n
"
, cwd.
c_str
());
fprintf
(f,
"
%s >
\"
%s
\"
2>&1
\r\n
"
, cmd.
c_str
(), out_file.
c_str
());
fprintf
(f,
"
exit /b %%ERRORLEVEL%%
\r\n
"
);
fclose
(f);
std::string command_line =
"
cmd.exe /C
\"
"
+ bat_file +
"
\"
"
;
std::vector<
char
>
cmdline
(command_line.
begin
(), command_line.
end
());
cmdline.
push_back
(
'
\0
'
);
STARTUPINFOA
si;
PROCESS_INFORMATION
pi;
ZeroMemory
(&si,
sizeof
(si));
ZeroMemory
(&pi,
sizeof
(pi));
si.
cb
=
sizeof
(si);
si.
dwFlags
=
STARTF_USESHOWWINDOW
;
si.
wShowWindow
=
SW_HIDE
;
BOOL
ok =
CreateProcessA
(
NULL
,
cmdline.
data
(),
NULL
,
NULL
,
FALSE
,
CREATE_NO_WINDOW
,
NULL
,
cwd.
empty
() ?
NULL
: cwd.
c_str
(),
&si,
&pi
);
int
ret = -
1
;
if
(!ok) {
DeleteFileA
(bat_file.
c_str
());
return
-
1
;
}
DWORD
wait_ret =
WaitForSingleObject
(pi.
hProcess
, timeout_ms ==
0
?
INFINITE
: timeout_ms);
if
(wait_ret ==
WAIT_TIMEOUT
) {
if
(timed_out) *timed_out =
true
;
TerminateProcess
(pi.
hProcess
,
124
);
WaitForSingleObject
(pi.
hProcess
,
INFINITE
);
ret =
124
;
}
else
{
DWORD
exit_code =
0
;
if
(
GetExitCodeProcess
(pi.
hProcess
, &exit_code)) {
ret =
static_cast
<
int
>(exit_code);
}
}
CloseHandle
(pi.
hThread
);
CloseHandle
(pi.
hProcess
);
DeleteFileA
(bat_file.
c_str
());
std::ifstream
in
(out_file, std::ios::binary);
if
(in.
is_open
()) {
std::ostringstream ss;
ss << in.
rdbuf
();
output = ss.
str
();
in.
close
();
DeleteFileA
(out_file.
c_str
());
}
else
{
output.
clear
();
}
return
ret;
}
static
std::string
find_adb
() {
std::string adb =
search_path_binary
(
"
adb.exe
"
);
if
(!adb.
empty
())
return
adb;
const
char
* envs[] = {
"
ANDROID_SDK_ROOT
"
,
"
ANDROID_HOME
"
};
for
(
auto
env : envs) {
std::string root =
get_env_var
(env);
if
(root.
empty
())
continue
;
std::string candidate = root +
"
\\
platform-tools
\\
adb.exe
"
;
if
(
file_exists
(candidate))
return
candidate;
}
return
"
"
;
}
static
std::string
find_test_device
(
const
std::string& adb_path) {
std::string output;
std::string cmd =
"
\"
"
+ adb_path +
"
\"
devices
"
;
if
(
run_cmd_capture
(cmd, output) !=
0
)
return
"
"
;
std::istringstream
iss
(output);
std::string line;
std::string first_device;
while
(
std::getline
(iss, line)) {
line =
trim_copy
(line);
if
(line.
empty
())
continue
;
if
(line.
find
(
"
List of devices attached
"
) ==
0
)
continue
;
if
(line.
find
(
"
* daemon
"
) ==
0
)
continue
;
size_t
tab = line.
find
(
'
\t
'
);
if
(tab == std::string::npos)
continue
;
std::string serial =
trim_copy
(line.
substr
(
0
, tab));
std::string state =
trim_copy
(line.
substr
(tab +
1
));
if
(state !=
"
device
"
)
continue
;
if
(serial.
find
(
"
emulator-
"
) ==
0
)
return
serial;
if
(first_device.
empty
()) first_device = serial;
}
return
first_device;
}
static
std::string
normalize_abi
(
const
std::string& abi) {
if
(abi.
find
(
"
arm64-v8a
"
) != std::string::npos)
return
"
arm64-v8a
"
;
if
(abi.
find
(
"
armeabi-v7a
"
) != std::string::npos)
return
"
armeabi-v7a
"
;
if
(abi.
find
(
"
x86_64
"
) != std::string::npos)
return
"
x86_64
"
;
if
(abi.
find
(
"
x86
"
) != std::string::npos)
return
"
x86
"
;
return
trim_copy
(abi);
}
static
std::string
detect_device_abi
(
const
std::string& adb_path,
const
std::string& serial) {
std::string output;
std::string cmd =
"
\"
"
+ adb_path +
"
\"
-s
"
+ serial +
"
shell getprop ro.product.cpu.abi
"
;
if
(
run_cmd_capture
(cmd, output) !=
0
)
return
"
"
;
std::istringstream
iss
(output);
std::string line;
while
(
std::getline
(iss, line)) {
line =
normalize_abi
(line);
if
(!line.
empty
())
return
line;
}
return
"
"
;
}
static
std::string
find_test_binary
(
const
std::string& project_dir,
const
std::string& abi) {
const
std::string candidates[] = {
project_dir +
"
\\
libs
\\
"
+ abi +
"
\\
allvm_test
"
,
project_dir +
"
\\
obj
\\
local
\\
"
+ abi +
"
\\
allvm_test
"
};
for
(
const
auto
& candidate : candidates) {
if
(
file_exists
(candidate))
return
candidate;
}
return
"
"
;
}
static
std::string
choose_device_test_path
(
const
std::string& adb_path,
const
std::string& serial,
const
std::string& binary_name) {
const
char
* dirs[] = {
"
/data/local/tmp
"
,
"
/data/local/tests
"
};
for
(
const
char
* dir : dirs) {
std::string probe =
std::string
(dir) +
"
/.allvm_probe
"
;
std::string cmd =
"
\"
"
+ adb_path +
"
\"
-s
"
+ serial +
"
shell
\"
touch
"
+ probe +
"
>/dev/null 2>&1 && rm -f
"
+ probe +
"
>/dev/null 2>&1
\"
"
;
std::string output;
if
(
run_cmd_capture
(cmd, output, g_script_dir) ==
0
) {
return
std::string
(dir) +
"
/
"
+ binary_name;
}
}
return
"
/data/local/tmp/
"
+ binary_name;
}
static
std::string
find_named_binary
(
const
std::string& project_dir,
const
std::string& abi,
const
std::string& binary_name_or_path) {
if
(binary_name_or_path.
empty
())
return
"
"
;
if
(
file_exists
(binary_name_or_path)) {
return
get_full_path_copy
(binary_name_or_path);
}
const
std::string candidates[] = {
project_dir +
"
\\
libs
\\
"
+ abi +
"
\\
"
+ binary_name_or_path,
project_dir +
"
\\
obj
\\
local
\\
"
+ abi +
"
\\
"
+ binary_name_or_path
};
for
(
const
auto
& candidate : candidates) {
if
(
file_exists
(candidate))
return
get_full_path_copy
(candidate);
}
return
"
"
;
}
static
std::string
lower_ascii_copy
(std::string value) {
std::transform
(value.
begin
(), value.
end
(), value.
begin
(),
[](
unsigned
char
c) {
return
static_cast
<
char
>(
std::tolower
(c)); });
return
value;
}
static
std::string
normalize_ascii_whitespace
(std::string value) {
value =
lower_ascii_copy
(
std::move
(value));
std::string out;
out.
reserve
(value.
size
());
bool
last_was_space =
false
;
for
(
unsigned
char
c : value) {
if
(
std::isspace
(c)) {
if
(!last_was_space) out.
push_back
(
'
'
);
last_was_space =
true
;
}
else
{
out.
push_back
(
static_cast
<
char
>(c));
last_was_space =
false
;
}
}
return
out;
}
static
bool
verify_frame_record_codegen
() {
std::string clangxx = g_build_dir +
"
\\
bin
\\
clang++.exe
"
;
if
(!
file_exists
(clangxx)) {
printf
(
"
[Warning] clang++ not found, skipping frame record verification
\n
"
);
return
true
;
}
std::string src = g_script_dir +
"
\\
test
\\
jni
\\
frame_record_codegen.cpp
"
;
std::string asm_path = g_script_dir +
"
\\
test
\\
frame_record_codegen.s
"
;
if
(!
file_exists
(src)) {
printf
(
"
[Error] Frame record probe source not found: %s
\n
"
, src.
c_str
());
return
false
;
}
std::string output;
std::string cmd =
"
\"
"
+ clangxx +
"
\"
--target=aarch64-linux-android21 -S -O2
"
"
-fno-omit-frame-pointer
"
"
-mllvm -aarch64-obfuscate-frame-record
"
"
-mllvm -aarch64-enable-ldst-opt=false
"
"
\"
"
+ src +
"
\"
-o
\"
"
+ asm_path +
"
\"
"
;
if
(
run_cmd_capture
(cmd, output) !=
0
) {
if
(!output.
empty
())
printf
(
"
%s
"
, output.
c_str
());
printf
(
"
[Error] Failed to compile frame record probe
\n
"
);
return
false
;
}
std::ifstream
in
(asm_path, std::ios::binary);
if
(!in.
is_open
()) {
printf
(
"
[Error] Failed to read generated asm: %s
\n
"
, asm_path.
c_str
());
return
false
;
}
std::ostringstream ss;
ss << in.
rdbuf
();
std::string asm_text = ss.
str
();
in.
close
();
std::istringstream
iss
(asm_text);
std::string line;
std::string block;
bool
in_symbol =
false
;
while
(
std::getline
(iss, line)) {
if
(!in_symbol) {
if
(line.
find
(
"
frame_record_probe:
"
) != std::string::npos) {
in_symbol =
true
;
block += line +
"
\n
"
;
}
continue
;
}
bool
is_new_symbol = !line.
empty
() &&
line[
0
] !=
'
\t
'
&&
line[
0
] !=
'
'
&&
line[
0
] !=
'
/
'
&&
line.
back
() ==
'
:
'
&&
line.
find
(
"
frame_record_probe:
"
) == std::string::npos;
if
(is_new_symbol || line.
rfind
(
"
.Lfunc_end
"
,
0
) ==
0
) {
break
;
}
block += line +
"
\n
"
;
}
if
(block.
empty
()) {
printf
(
"
[Error] frame_record_probe symbol not found in generated asm
\n
"
);
return
false
;
}
std::replace
(block.
begin
(), block.
end
(),
'
\t
'
,
'
'
);
std::string lower =
lower_ascii_copy
(block);
auto
has = [&](
const
char
* needle) {
return
lower.
find
(needle) != std::string::npos;
};
bool
has_split_store = (
has
(
"
str x29
"
) ||
has
(
"
stur x29
"
)) &&
(
has
(
"
str x30
"
) ||
has
(
"
stur x30
"
));
bool
has_split_load = (
has
(
"
ldr x29
"
) ||
has
(
"
ldur x29
"
)) &&
(
has
(
"
ldr x30
"
) ||
has
(
"
ldur x30
"
));
bool
has_fp_mix =
has
(
"
add x29, sp
"
) &&
has
(
"
sub x29, x29
"
);
bool
has_std_pair =
has
(
"
stp x29, x30
"
) ||
has
(
"
stp x30, x29
"
) ||
has
(
"
ldp x29, x30
"
) ||
has
(
"
ldp x30, x29
"
);
bool
has_std_mov =
has
(
"
mov x29, sp
"
);
if
(!has_split_store || !has_split_load || !has_fp_mix || has_std_pair || has_std_mov) {
printf
(
"
[Error] frame_record_probe codegen verification failed
\n
"
);
printf
(
"
-------- frame_record_probe --------
\n
%s-------- end --------
\n
"
, block.
c_str
());
return
false
;
}
printf
(
"
[Done] frame_record_probe uses split frame record sequence
\n
"
);
return
true
;
}
static
bool
verify_call_ret_codegen
() {
std::string clangxx = g_build_dir +
"
\\
bin
\\
clang++.exe
"
;
if
(!
file_exists
(clangxx)) {
printf
(
"
[Warning] clang++ not found, skipping call/ret verification
\n
"
);
return
true
;
}
std::string src = g_script_dir +
"
\\
test
\\
jni
\\
call_ret_codegen.cpp
"
;
std::string asm_path = g_script_dir +
"
\\
test
\\
call_ret_codegen.s
"
;
if
(!
file_exists
(src)) {
printf
(
"
[Error] Call/ret probe source not found: %s
\n
"
, src.
c_str
());
return
false
;
}
std::string output;
std::string cmd =
"
\"
"
+ clangxx +
"
\"
--target=aarch64-linux-android21 -S -O2
"
"
-mllvm -aarch64-obfuscate-call-ret
"
"
\"
"
+ src +
"
\"
-o
\"
"
+ asm_path +
"
\"
"
;
if
(
run_cmd_capture
(cmd, output) !=
0
) {
if
(!output.
empty
())
printf
(
"
%s
"
, output.
c_str
());
printf
(
"
[Error] Failed to compile call/ret probe
\n
"
);
return
false
;
}
std::ifstream
in
(asm_path, std::ios::binary);
if
(!in.
is_open
()) {
printf
(
"
[Error] Failed to read generated asm: %s
\n
"
, asm_path.
c_str
());
return
false
;
}
std::ostringstream ss;
ss << in.
rdbuf
();
std::string asm_text = ss.
str
();
in.
close
();
std::istringstream
iss
(asm_text);
std::string line;
std::string block;
bool
in_symbol =
false
;
while
(
std::getline
(iss, line)) {
if
(!in_symbol) {
if
(line.
find
(
"
call_ret_probe:
"
) != std::string::npos) {
in_symbol =
true
;
block += line +
"
\n
"
;
}
continue
;
}
bool
is_new_symbol = !line.
empty
() &&
line[
0
] !=
'
\t
'
&&
line[
0
] !=
'
'
&&
line[
0
] !=
'
/
'
&&
line.
back
() ==
'
:
'
&&
line.
find
(
"
call_ret_probe:
"
) == std::string::npos;
if
(is_new_symbol || line.
rfind
(
"
.Lfunc_end
"
,
0
) ==
0
) {
break
;
}
block += line +
"
\n
"
;
}
if
(block.
empty
()) {
printf
(
"
[Error] call_ret_probe symbol not found in generated asm
\n
"
);
return
false
;
}
std::replace
(block.
begin
(), block.
end
(),
'
\t
'
,
'
'
);
std::string lower =
lower_ascii_copy
(block);
auto
has = [&](
const
char
* needle) {
return
lower.
find
(needle) != std::string::npos;
};
bool
has_target_materialization =
has
(
"
adrp
"
) &&
has
(
"
target_call
"
) &&
(
has
(
"
:lo12:target_call
"
) ||
has
(
"
target_call
"
));
bool
has_indirect_call = has_target_materialization &&
has
(
"
blr x
"
);
bool
has_br_lr =
has
(
"
br x30
"
);
bool
has_direct_bl =
has
(
"
bl target_call
"
);
bool
has_ret =
has
(
"
\n
ret
"
) ||
has
(
"
\n
ret
"
);
if
(!has_indirect_call || !has_br_lr || has_direct_bl || has_ret) {
printf
(
"
[Error] call_ret_probe codegen verification failed
\n
"
);
printf
(
"
-------- call_ret_probe --------
\n
%s-------- end --------
\n
"
, block.
c_str
());
return
false
;
}
printf
(
"
[Done] call_ret_probe uses indirect call and BR x30 sequence
\n
"
);
return
true
;
}
static
bool
verify_call_ret_scratch_mir
() {
std::string llc = g_build_dir +
"
\\
bin
\\
llc.exe
"
;
if
(!
file_exists
(llc)) {
printf
(
"
[Warning] llc not found, skipping call/ret scratch verification
\n
"
);
return
true
;
}
std::string src = g_script_dir +
"
\\
llvm
\\
test
\\
CodeGen
\\
AArch64
\\
irobf-call-ret-scratch.mir
"
;
if
(!
file_exists
(src)) {
printf
(
"
[Error] Call/ret scratch MIR not found: %s
\n
"
, src.
c_str
());
return
false
;
}
std::string output;
std::string cmd =
"
\"
"
+ llc +
"
\"
-mtriple=aarch64-none-linux-gnu
"
"
-run-pass=aarch64-call-ret-obfuscation
"
"
-aarch64-obfuscate-call-ret
"
"
-verify-machineinstrs=0 -o -
"
"
\"
"
+ src +
"
\"
"
;
int
mir_ret =
run_cmd_capture
(cmd, output);
dbg_log
(
"
mir_verify_call_ret_scratch_start
"
, cmd);
if
(output.
empty
()) {
printf
(
"
[Error] Failed to run call/ret scratch MIR verification
\n
"
);
dbg_log
(
"
mir_verify_call_ret_scratch_error
"
,
"
empty_output
"
);
return
false
;
}
if
(mir_ret !=
0
&& !output.
empty
()) {
printf
(
"
%s
"
, output.
c_str
());
//
Continue to analyze output even if llc returned non-zero,
//
as long as the expected patterns are present.
dbg_log
(
"
mir_verify_call_ret_scratch_nonzero
"
,
std::string
(
"
code=
"
) +
std::to_string
(mir_ret));
}
std::string lower =
lower_ascii_copy
(output);
auto
has = [&](
const
char
* needle) {
return
lower.
find
(needle) != std::string::npos;
};
bool
has_indirect_call =
has
(
"
adrp
"
) &&
has
(
"
addxri
"
) &&
has
(
"
blr
"
);
bool
preserves_x16_arg =
has
(
"
implicit $x16
"
);
bool
clobbers_x16 =
has
(
"
$x16 = adrp
"
) ||
has
(
"
$x16 = addxri
"
) ||
has
(
"
blr $x16
"
);
if
(!has_indirect_call || !preserves_x16_arg || clobbers_x16) {
printf
(
"
[Error] call/ret scratch MIR verification failed
\n
"
);
printf
(
"
-------- irobf-call-ret-scratch.mir --------
\n
%s-------- end --------
\n
"
, output.
c_str
());
dbg_log
(
"
mir_verify_call_ret_scratch_fail
"
,
std::string
(
"
has_indirect_call=
"
) + (has_indirect_call?
"
1
"
:
"
0
"
) +
"
preserves_x16_arg=
"
+ (preserves_x16_arg?
"
1
"
:
"
0
"
) +
"
clobbers_x16=
"
+ (clobbers_x16?
"
1
"
:
"
0
"
));
return
false
;
}
printf
(
"
[Done] scratch MIR keeps x16 live and uses a different call scratch register
\n
"
);
dbg_log
(
"
mir_verify_call_ret_scratch_ok
"
,
"
ok
"
);
return
true
;
}
static
bool
verify_opaque_predicate_codegen
() {
std::string llc = g_build_dir +
"
\\
bin
\\
llc.exe
"
;
if
(!
file_exists
(llc)) {
printf
(
"
[Warning] llc not found, skipping opaque predicate verification
\n
"
);
return
true
;
}
std::string objdump = g_build_dir +
"
\\
bin
\\
llvm-objdump.exe
"
;
if
(!
file_exists
(objdump)) {
printf
(
"
[Warning] llvm-objdump not found, skipping opaque predicate object verification
\n
"
);
return
true
;
}
std::string src = g_script_dir +
"
\\
llvm
\\
test
\\
CodeGen
\\
AArch64
\\
irobf-opaque-predicate-bytes.ll
"
;
if
(!
file_exists
(src)) {
printf
(
"
[Error] Opaque predicate probe source not found: %s
\n
"
, src.
c_str
());
return
false
;
}
std::string asm_path = g_script_dir +
"
\\
test
\\
opaque_predicate_codegen.s
"
;
std::string obj_path = g_script_dir +
"
\\
test
\\
opaque_predicate_codegen.o
"
;
std::string output;
std::string asm_cmd =
"
\"
"
+ llc +
"
\"
-mtriple=aarch64-linux-gnu -O2
"
"
-aarch64-obfuscate-opaque-predicate
"
"
\"
"
+ src +
"
\"
-o
\"
"
+ asm_path +
"
\"
"
;
if
(
run_cmd_capture
(asm_cmd, output) !=
0
) {
if
(!output.
empty
())
printf
(
"
%s
"
, output.
c_str
());
printf
(
"
[Error] Failed to compile opaque predicate asm probe
\n
"
);
return
false
;
}
std::ifstream
in
(asm_path, std::ios::binary);
if
(!in.
is_open
()) {
printf
(
"
[Error] Failed to read generated asm: %s
\n
"
, asm_path.
c_str
());
return
false
;
}
std::ostringstream ss;
ss << in.
rdbuf
();
std::string asm_text = ss.
str
();
in.
close
();
std::string lower_asm =
normalize_ascii_whitespace
(asm_text);
auto
asm_has = [&](
const
char
* needle) {
return
lower_asm.
find
(needle) != std::string::npos;
};
bool
has_guard_branch =
asm_has
(
"
cbnz x9
"
);
bool
has_dead_bytes =
asm_has
(
"
.byte 0
"
);
bool
has_misaligned_tail =
asm_has
(
"
.byte 232
"
);
bool
has_realign =
asm_has
(
"
.p2align 2, 0x0
"
);
if
(!has_guard_branch || !has_dead_bytes || !has_misaligned_tail || !has_realign) {
printf
(
"
[Error] opaque predicate asm verification failed
\n
"
);
printf
(
"
-------- opaque_predicate_codegen.s --------
\n
%s-------- end --------
\n
"
, asm_text.
c_str
());
return
false
;
}
output.
clear
();
std::string obj_cmd =
"
\"
"
+ llc +
"
\"
-mtriple=aarch64-linux-gnu -filetype=obj -O2
"
"
-aarch64-obfuscate-opaque-predicate
"
"
\"
"
+ src +
"
\"
-o
\"
"
+ obj_path +
"
\"
"
;
if
(
run_cmd_capture
(obj_cmd, output) !=
0
) {
if
(!output.
empty
())
printf
(
"
%s
"
, output.
c_str
());
printf
(
"
[Error] Failed to compile opaque predicate object probe
\n
"
);
return
false
;
}
output.
clear
();
std::string dump_cmd =
"
\"
"
+ objdump +
"
\"
-d --triple=aarch64-linux-gnu
"
"
\"
"
+ obj_path +
"
\"
"
;
if
(
run_cmd_capture
(dump_cmd, output) !=
0
) {
if
(!output.
empty
())
printf
(
"
%s
"
, output.
c_str
());
printf
(
"
[Error] Failed to disassemble opaque predicate object probe
\n
"
);
return
false
;
}
std::string lower_dump =
normalize_ascii_whitespace
(output);
auto
dump_has = [&](
const
char
* needle) {
return
lower_dump.
find
(needle) != std::string::npos;
};
bool
has_udf_encoding =
dump_has
(
"
.word 0x00000000
"
);
bool
has_tail_word =
dump_has
(
"
.word 0x000003e8
"
);
if
(!has_udf_encoding || !has_tail_word) {
printf
(
"
[Error] opaque predicate object verification failed
\n
"
);
printf
(
"
-------- opaque_predicate_codegen.o --------
\n
%s-------- end --------
\n
"
, output.
c_str
());
return
false
;
}
printf
(
"
[Done] opaque predicate emits dead bytes with UDF encoding and misaligned tail
\n
"
);
return
true
;
}
static
bool
build_ndk_test_project
(
const
TestRunOptions& opts,
int
jobs) {
std::string ndk_build_bat = g_ndk_dir +
"
\\
ndk-build.bat
"
;
std::string ndk_build_cmd = g_ndk_dir +
"
\\
ndk-build.cmd
"
;
if
(g_ndk_dir.
empty
() || (!
file_exists
(ndk_build_bat) && !
file_exists
(ndk_build_cmd))) {
printf
(
"
[Error] NDK not found, cannot build test project
\n
"
);
return
false
;
}
std::string project_dir =
get_full_path_copy
(opts.
project_dir
);
if
(!
dir_exists
(project_dir)) {
printf
(
"
[Error] Test project directory not found: %s
\n
"
, project_dir.
c_str
());
return
false
;
}
if
(opts.
clean_test
) {
run_cmd
(
"
if exist obj rmdir /s /q obj
"
, project_dir);
run_cmd
(
"
if exist libs rmdir /s /q libs
"
, project_dir);
}
char
jbuf[
16
];
sprintf
(jbuf,
"
%d
"
, jobs);
std::string ndk_build =
file_exists
(ndk_build_bat) ? ndk_build_bat : ndk_build_cmd;
std::string ndk_cmd =
"
\"
"
+ ndk_build +
"
\"
NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=
\"
"
+
opts.
app_build_script
+
"
\"
NDK_APPLICATION_MK=
\"
"
+
opts.
app_application_mk
+
"
\"
-j
"
+ jbuf;
dbg_log
(
"
ndk_build_start
"
,
std::string
(
"
cwd=
"
) + project_dir +
"
cmd=
"
+ ndk_cmd);
int
ret =
run_cmd
(ndk_cmd, project_dir);
dbg_log
(
"
ndk_build_end
"
,
std::string
(
"
ret=
"
) +
std::to_string
(ret));
if
(ret !=
0
) {
printf
(
"
[Error] ndk-build failed for %s (code: %d)
\n
"
, project_dir.
c_str
(), ret);
return
false
;
}
return
true
;
}
static
bool
push_and_run_binary
(
const
TestRunOptions& opts) {
std::string adb_path =
find_adb
();
if
(adb_path.
empty
()) {
printf
(
"
[Error] adb not found in PATH / ANDROID_SDK_ROOT / ANDROID_HOME
\n
"
);
return
false
;
}
std::string serial = opts.
serial
.
empty
() ?
find_test_device
(adb_path) : opts.
serial
;
if
(serial.
empty
()) {
printf
(
"
[Error] No connected emulator/device found for adb testing
\n
"
);
return
false
;
}
printf
(
"
-> Using device: %s
\n
"
, serial.
c_str
());
std::string abi = opts.
abi
.
empty
() ?
detect_device_abi
(adb_path, serial) :
normalize_abi
(opts.
abi
);
if
(abi.
empty
()) {
printf
(
"
[Error] Failed to detect device ABI
\n
"
);
return
false
;
}
printf
(
"
-> Device ABI: %s
\n
"
, abi.
c_str
());
std::string local_binary = opts.
local_binary_path
;
if
(local_binary.
empty
()) {
local_binary =
find_named_binary
(
get_full_path_copy
(opts.
project_dir
), abi, opts.
binary_name
);
}
else
{
local_binary =
get_full_path_copy
(local_binary);
}
if
(local_binary.
empty
() || !
file_exists
(local_binary)) {
printf
(
"
[Error] Test binary not found
\n
"
);
return
false
;
}
std::string device_path = opts.
device_path
;
if
(device_path.
empty
()) {
device_path =
choose_device_test_path
(adb_path, serial,
path_basename
(local_binary));
}
printf
(
"
-> Local binary: %s
\n
"
, local_binary.
c_str
());
printf
(
"
-> Device path : %s
\n
"
, device_path.
c_str
());
std::string output;
std::string push_cmd =
"
\"
"
+ adb_path +
"
\"
-s
"
+ serial +
"
push
\"
"
+
local_binary +
"
\"
\"
"
+ device_path +
"
\"
"
;
int
ret =
run_cmd_capture
(push_cmd, output);
if
(!output.
empty
())
printf
(
"
%s
"
, output.
c_str
());
if
(ret !=
0
) {
printf
(
"
[Error] adb push failed (code: %d)
\n
"
, ret);
return
false
;
}
std::string remote_run = opts.
run_command
.
empty
()
? (
"
chmod 755
\"
"
+ device_path +
"
\"
&&
\"
"
+ device_path +
"
\"
"
)
: opts.
run_command
;
bool
timed_out =
false
;
output.
clear
();
std::string exec_cmd =
"
\"
"
+ adb_path +
"
\"
-s
"
+ serial +
"
shell
\"
"
+ remote_run +
"
\"
"
;
ret =
run_cmd_capture_timeout
(exec_cmd, output, g_script_dir,
opts.
timeout_sec
>
0
?
static_cast
<
DWORD
>(opts.
timeout_sec
) *
1000U
:
0U
,
&timed_out);
if
(!output.
empty
())
printf
(
"
%s
"
, output.
c_str
());
if
(timed_out) {
printf
(
"
[Error] Device run timed out after %d seconds
\n
"
, opts.
timeout_sec
);
std::string base =
path_basename
(device_path);
std::string kill_cmd =
"
\"
"
+ adb_path +
"
\"
-s
"
+ serial +
"
shell
\"
pkill -f '
"
+ base +
"
' >/dev/null 2>&1 || true
\"
"
;
std::string kill_output;
run_cmd_capture
(kill_cmd, kill_output, g_script_dir);
return
false
;
}
if
(ret !=
0
) {
printf
(
"
[Error] Device run failed (code: %d)
\n
"
, ret);
return
false
;
}
return
true
;
}
static
bool
ensure_fla_test_project_exists
() {
std::string jni_dir = g_script_dir +
"
\\
test
\\
jni
"
;
const
char
* required[] = {
"
Android.mk
"
,
"
Application.mk
"
,
"
main.cpp
"
};
for
(
const
char
* name : required) {
std::string path = jni_dir +
"
\\
"
+ name;
if
(!
file_exists
(path)) {
printf
(
"
[Error] Missing handwritten test file: %s
\n
"
, path.
c_str
());
return
false
;
}
}
return
true
;
}
static
bool
build_and_run_fla_test
(
int
jobs,
const
TestRunOptions& opts) {
printf
(
"
\n
[Test] Building handwritten ALLVM_TEST project...
\n
"
);
std::string ndk_build_bat = g_ndk_dir +
"
\\
ndk-build.bat
"
;
std::string ndk_build_cmd = g_ndk_dir +
"
\\
ndk-build.cmd
"
;
if
(g_ndk_dir.
empty
() || (!
file_exists
(ndk_build_bat) && !
file_exists
(ndk_build_cmd))) {
printf
(
"
[Error] NDK not found, cannot run emulator test
\n
"
);
return
false
;
}
std::string adb_path =
find_adb
();
if
(adb_path.
empty
()) {
printf
(
"
[Error] adb not found in PATH / ANDROID_SDK_ROOT / ANDROID_HOME
\n
"
);
return
false
;
}
std::string serial = opts.
serial
.
empty
() ?
find_test_device
(adb_path) : opts.
serial
;
if
(serial.
empty
()) {
printf
(
"
[Error] No connected emulator/device found for adb testing
\n
"
);
return
false
;
}
printf
(
"
-> Using device: %s
\n
"
, serial.
c_str
());
std::string abi = opts.
abi
.
empty
() ?
detect_device_abi
(adb_path, serial) :
normalize_abi
(opts.
abi
);
if
(abi.
empty
()) {
printf
(
"
[Error] Failed to detect device ABI
\n
"
);
return
false
;
}
printf
(
"
-> Test ABI: %s
\n
"
, abi.
c_str
());
if
(!
ensure_fla_test_project_exists
()) {
return
false
;
}
std::string test_dir = g_script_dir +
"
\\
test
"
;
if
(opts.
clean_test
) {
run_cmd
(
"
if exist obj rmdir /s /q obj
"
, test_dir);
run_cmd
(
"
if exist libs rmdir /s /q libs
"
, test_dir);
}
char
jbuf[
16
];
sprintf
(jbuf,
"
%d
"
, jobs);
std::string ndk_build =
file_exists
(ndk_build_bat) ? ndk_build_bat : ndk_build_cmd;
std::string ndk_cmd =
"
\"
"
+ ndk_build +
"
\"
NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=jni/Android.mk NDK_APPLICATION_MK=jni/Application.mk APP_ABI=
"
+ abi +
"
-j
"
+ jbuf;
dbg_log
(
"
ndk_build_start
"
,
std::string
(
"
cwd=
"
) + test_dir +
"
cmd=
"
+ ndk_cmd);
int
ret =
run_cmd
(ndk_cmd, test_dir);
dbg_log
(
"
ndk_build_end
"
,
std::string
(
"
ret=
"
) +
std::to_string
(ret));
if
(ret !=
0
) {
printf
(
"
[Error] ndk-build test project failed (code: %d)
\n
"
, ret);
return
false
;
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL