这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit 9099111

Browse files
Added: Add libtermux-core_c and libtermux-core_cxx
Both static and dynamic versions of the libraries are provided and they can be used internally for `termux-core` executables/libraries, and also for other packages. Various standardized and safe utils are provided, the primary ones are listed below. - Added logger framework with multiple log levels that supports writing logs to standard streams and to a file, or a custom implementation. The log level can be read from an environment variable by calling `getLogLevelFromEnv()` which maps to `(OFF=0, NORMAL=1, DEBUG=2, VERBOSE=3, VVERBOSE=4 and VVVERBOSE=5)`. The default value is `1`. - Added various file utils including normalize and absolutize path functions from `portal-io` library and AOSP that has been tested on hundreds of test cases. - Add Termux shell environment headers for Unix/Android/Termux variable names, and for reading Termux scoped environment variable values with a fallback for build time values, like for `TERMUX_APP__DATA_DIR`, `TERMUX__ROOTFS`, `TERMUX__PREFIX`, etc.
1 parent 4834bb0 commit 9099111

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+6569
-2
lines changed

Makefile

Lines changed: 166 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,70 @@ FSANTIZE_FLAGS += -fsanitize=address -fsanitize-recover=address -fno-omit-frame-
225225

226226

227227

228+
override LIBTERMUX_CORE__C__SOURCE_FILES := \
229+
lib/termux-core_c/src/TermuxCoreLibraryConfig.c \
230+
lib/termux-core_c/src/data/AssertUtils.c \
231+
lib/termux-core_c/src/data/DataUtils.c \
232+
lib/termux-core_c/src/file/FileUtils.c \
233+
lib/termux-core_c/src/logger/FileLoggerImpl.c \
234+
lib/termux-core_c/src/logger/Logger.c \
235+
lib/termux-core_c/src/logger/StandardLoggerImpl.c \
236+
lib/termux-core_c/src/os/process/ForkUtils.c \
237+
lib/termux-core_c/src/os/process/SafeStrerror.c \
238+
lib/termux-core_c/src/os/process/SignalUtils.c \
239+
lib/termux-core_c/src/os/selinux/SeLinuxUtils.c \
240+
lib/termux-core_c/src/security/SecurityUtils.c \
241+
lib/termux-core_c/src/shell/command/environment/AndroidShellEnvironment.c \
242+
lib/termux-core_c/src/shell/command/environment/UnixShellEnvironment.c \
243+
lib/termux-core_c/src/termux/file/TermuxFile.c \
244+
lib/termux-core_c/src/termux/shell/command/environment/TermuxShellEnvironment.c \
245+
lib/termux-core_c/src/termux/shell/command/environment/termux_core/TermuxCoreShellEnvironment.c
246+
247+
override LIBTERMUX_CORE__C__OBJECT_FILES := $(patsubst lib/%.c,$(TMP_BUILD_OUTPUT_DIR)/lib/%.o,$(LIBTERMUX_CORE__C__SOURCE_FILES))
248+
249+
override LIBTERMUX_CORE__C__CPPFLAGS := $(CPPFLAGS) -I "lib/termux-core_c/include"
250+
251+
override LIBTERMUX_CORE__C__TESTS_BUILD_OUTPUT_DIR := $(TESTS_BUILD_OUTPUT_DIR)/lib/termux-core_c
252+
253+
254+
255+
override LIBTERMUX_CORE__CXX__SOURCE_FILES := \
256+
lib/termux-core_cxx/src/TermuxCoreLibraryConfig.cxx
257+
258+
override LIBTERMUX_CORE__CXX__OBJECT_FILES := $(patsubst lib/%.cxx,$(TMP_BUILD_OUTPUT_DIR)/lib/%.o,$(LIBTERMUX_CORE__CXX__SOURCE_FILES))
259+
260+
override LIBTERMUX_CORE__CXX__CPPFLAGS := $(CPPFLAGS) -I "lib/termux-core_c/include" -I "lib/termux-core_cxx/include"
261+
262+
263+
264+
# The `-L` flag must come before `$LDFLAGS`, otherwise old library
265+
# installed in system library directory from previous builds
266+
# will get used instead of newly built one in `$LIB_BUILD_OUTPUT_DIR`.
267+
# The `-fvisibility=hidden` flag is passed so that no internal
268+
# functions are exported. All exported functions must explicitly enable
269+
# `default` visibility with `__attribute__((visibility("default")))`,
270+
# like for the `main()` function.
271+
# The `-Wl,--exclude-libs=ALL` flag is passed so that symbols from
272+
# the `libtermux-core_c.a` static library linked are not exported.
273+
# Run `nm --demangle --defined-only --extern-only <executable>` to
274+
# find exported symbols.
275+
override TERMUX_CORE_EXECUTABLE__C__BUILD_COMMAND := \
276+
$(CC) $(CFLAGS) $(LIBTERMUX_CORE__C__CPPFLAGS) \
277+
-L$(LIB_BUILD_OUTPUT_DIR) $(LDFLAGS) -Wl,--exclude-libs=ALL \
278+
$(TERMUX__CONSTANTS__MACRO_FLAGS) \
279+
-fPIE -pie -fvisibility=hidden
280+
281+
# The `-l` flags must be passed after object files for proper linking.
282+
# The order of libraries matters too and any dependencies of a library
283+
# must come after it.
284+
override TERMUX_CORE_EXECUTABLE__C__POST_LDFLAGS := -l:libtermux-core_c.a
285+
286+
287+
228288
# - https://www.gnu.org/software/make/manual/html_node/Parallel-Disable.html
229289
.NOTPARALLEL:
230290

231-
all: | pre-build
291+
all: | pre-build build-libtermux-core_c build-libtermux-core_cxx
232292
@printf "\ntermux-core-package: %s\n" "Building scripts/*"
233293
@mkdir -p $(BIN_BUILD_OUTPUT_DIR)
234294
find scripts -type f -name "*.in" -exec sh -c \
@@ -270,6 +330,86 @@ pre-build: | clean
270330
@mkdir -p $(BUILD_OUTPUT_DIR)
271331
@mkdir -p $(TMP_BUILD_OUTPUT_DIR)
272332

333+
build-libtermux-core_c:
334+
@printf "\ntermux-core-package: %s\n" "Building lib/termux-core_c"
335+
@mkdir -p $(LIB_BUILD_OUTPUT_DIR)
336+
337+
@printf "\ntermux-core-package: %s\n" "Building lib/termux-core_c/lib/*.o"
338+
for source_file in $(LIBTERMUX_CORE__C__SOURCE_FILES); do \
339+
mkdir -p "$$(dirname "$(TMP_BUILD_OUTPUT_DIR)/$$source_file")" || exit $$?; \
340+
$(CC) -c $(CFLAGS) $(LIBTERMUX_CORE__C__CPPFLAGS) \
341+
$(TERMUX__CONSTANTS__MACRO_FLAGS) \
342+
-fPIC -fvisibility=default \
343+
-o $(TMP_BUILD_OUTPUT_DIR)/"$$(echo "$$source_file" | sed -E "s/(.*)\.c$$/\1.o/")" \
344+
"$$source_file" || exit $$?; \
345+
done
346+
347+
@# `nm --demangle --dynamic --defined-only --extern-only /home/builder/.termux-build/termux-core/src/build/output/usr/lib/libtermux-core_c.so`
348+
@printf "\ntermux-core-package: %s\n" "Building lib/libtermux-core_c.so"
349+
$(CC) $(CFLAGS) $(LIBTERMUX_CORE__C__CPPFLAGS) \
350+
$(LDFLAGS) \
351+
$(TERMUX__CONSTANTS__MACRO_FLAGS) \
352+
-fPIC -shared -fvisibility=default \
353+
-o $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_c.so \
354+
$(LIBTERMUX_CORE__C__OBJECT_FILES)
355+
356+
@printf "\ntermux-core-package: %s\n" "Building lib/libtermux-core_c.a"
357+
$(AR) rcs $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_c.a $(LIBTERMUX_CORE__C__OBJECT_FILES)
358+
359+
360+
361+
@printf "\ntermux-core-package: %s\n" "Building lib/termux-core_c/tests/*"
362+
@mkdir -p $(LIBTERMUX_CORE__C__TESTS_BUILD_OUTPUT_DIR)
363+
364+
365+
@printf "\ntermux-core-package: %s\n" "Building lib/termux-core_c/tests/libtermux-core_c_tests"
366+
$(call replace-termux-constants,lib/termux-core_c/tests/libtermux-core_c_tests,$(LIBTERMUX_CORE__C__TESTS_BUILD_OUTPUT_DIR))
367+
chmod 700 $(LIBTERMUX_CORE__C__TESTS_BUILD_OUTPUT_DIR)/libtermux-core_c_tests
368+
369+
370+
@printf "\ntermux-core-package: %s\n" "Building lib/termux-core_c/tests/bin/libtermux-core_c_unit-binary-tests"
371+
@mkdir -p $(LIBTERMUX_CORE__C__TESTS_BUILD_OUTPUT_DIR)/bin
372+
373+
@# `nm --demangle --defined-only --extern-only /home/builder/.termux-build/termux-core/src/build/output/usr/libexec/installed-tests/termux-core/lib/termux-core_c/bin/libtermux-core_c_unit-binary-tests-fsanitize`
374+
$(TERMUX_CORE_EXECUTABLE__C__BUILD_COMMAND) -O0 -g \
375+
$(FSANTIZE_FLAGS) \
376+
-o $(LIBTERMUX_CORE__C__TESTS_BUILD_OUTPUT_DIR)/bin/libtermux-core_c_unit-binary-tests-fsanitize \
377+
lib/termux-core_c/tests/src/libtermux-core_c_unit-binary-tests.c \
378+
$(TERMUX_CORE_EXECUTABLE__C__POST_LDFLAGS)
379+
380+
@# `nm --demangle --defined-only --extern-only /home/builder/.termux-build/termux-core/src/build/output/usr/libexec/installed-tests/termux-core/lib/termux-core_c/bin/libtermux-core_c_unit-binary-tests-nofsanitize`
381+
$(TERMUX_CORE_EXECUTABLE__C__BUILD_COMMAND) -O0 -g \
382+
-o $(LIBTERMUX_CORE__C__TESTS_BUILD_OUTPUT_DIR)/bin/libtermux-core_c_unit-binary-tests-nofsanitize \
383+
lib/termux-core_c/tests/src/libtermux-core_c_unit-binary-tests.c \
384+
$(TERMUX_CORE_EXECUTABLE__C__POST_LDFLAGS)
385+
386+
build-libtermux-core_cxx:
387+
@printf "\ntermux-core-package: %s\n" "Building lib/termux-core_cxx"
388+
@mkdir -p $(LIB_BUILD_OUTPUT_DIR)
389+
390+
@printf "\ntermux-core-package: %s\n" "Building lib/termux-core_cxx/lib/*.o"
391+
for source_file in $(LIBTERMUX_CORE__CXX__SOURCE_FILES); do \
392+
mkdir -p "$$(dirname "$(TMP_BUILD_OUTPUT_DIR)/$$source_file")" || exit $$?; \
393+
$(CXX) -c $(CXXFLAGS) $(LIBTERMUX_CORE__CXX__CPPFLAGS) \
394+
$(TERMUX__CONSTANTS__MACRO_FLAGS) \
395+
-fPIC -fvisibility=default \
396+
-o $(TMP_BUILD_OUTPUT_DIR)/"$$(echo "$$source_file" | sed -E "s/(.*)\.cxx$$/\1.o/")" \
397+
"$$source_file" || exit $$?; \
398+
done
399+
400+
@# `nm --demangle --dynamic --defined-only --extern-only /home/builder/.termux-build/termux-core/src/build/output/usr/lib/libtermux-core_cxx.so`
401+
@printf "\ntermux-core-package: %s\n" "Building lib/libtermux-core_cxx.so"
402+
$(CXX) $(CXXFLAGS) $(LIBTERMUX_CORE__CXX__CPPFLAGS) \
403+
-L$(LIB_BUILD_OUTPUT_DIR) $(LDFLAGS) -Wl,--exclude-libs=ALL \
404+
$(TERMUX__CONSTANTS__MACRO_FLAGS) \
405+
-fPIC -shared -fvisibility=default \
406+
-o $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_cxx.so \
407+
$(LIBTERMUX_CORE__CXX__OBJECT_FILES) \
408+
-l:libtermux-core_c.a
409+
410+
@printf "\ntermux-core-package: %s\n" "Building lib/libtermux-core_cxx.a"
411+
$(AR) rcs $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_cxx.a $(LIBTERMUX_CORE__CXX__OBJECT_FILES)
412+
273413

274414

275415
clean:
@@ -278,12 +418,28 @@ clean:
278418
install:
279419
@printf "termux-core-package: %s\n" "Installing termux-core-package in $(TERMUX_CORE_PKG__INSTALL_PREFIX)"
280420
install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/bin
421+
install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include
422+
install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib
281423
install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec
282424

283425

284426
find $(BIN_BUILD_OUTPUT_DIR) -maxdepth 1 \( -type f -o -type l \) -exec cp -a "{}" $(TERMUX_CORE_PKG__INSTALL_PREFIX)/bin/ \;
285427

286428

429+
rm -rf $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core
430+
install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core/termux
431+
432+
cp -a lib/termux-core_c/include/termux/termux_core__c $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core/termux/termux_core__c
433+
install $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_c.so $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_c.so
434+
install $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_c.a $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_c.a
435+
436+
cp -a lib/termux-core_cxx/include/termux/termux_core__cxx $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core/termux/termux_core__cxx
437+
install $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_cxx.so $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_cxx.so
438+
install $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_cxx.a $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_cxx.a
439+
440+
find $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core -type d -exec chmod 700 {} \;
441+
find $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core -type f -exec chmod 600 {} \;
442+
287443

288444
rm -rf $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-core
289445
install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests
@@ -297,6 +453,14 @@ uninstall:
297453
find scripts \( -type f -o -type l \) -exec sh -c \
298454
'rm -f "$(TERMUX_CORE_PKG__INSTALL_PREFIX)/bin/$$(basename "$$1" | sed "s/\.in$$//")"' sh "{}" \;
299455

456+
rm -rf $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core
457+
458+
459+
rm -f $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_c.so
460+
rm -f $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_c.a
461+
462+
rm -f $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_cxx.so
463+
rm -f $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_cxx.a
300464

301465

302466
rm -rf $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-core
@@ -339,4 +503,4 @@ test-runtime: all
339503

340504

341505

342-
.PHONY: all pre-build clean install uninstall packaging-debian-build test test-unit test-runtime
506+
.PHONY: all pre-build build-libtermux-core_c build-libtermux-core_cxx clean install uninstall packaging-debian-build test test-unit test-runtime
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef LIBTERMUX_CORE__C__TERMUX_CORE_LIBRARY_CONFIG___H
2+
#define LIBTERMUX_CORE__C__TERMUX_CORE_LIBRARY_CONFIG___H
3+
4+
#include <stdbool.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
11+
12+
/** Get `sIsRunningTests`. */
13+
bool libtermux_core__c__getIsRunningTests();
14+
15+
/** Set `sIsRunningTests`. */
16+
void libtermux_core__c__setIsRunningTests(bool isRunningTests);
17+
18+
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif
23+
24+
#endif // LIBTERMUX_CORE__C__TERMUX_CORE_LIBRARY_CONFIG___H
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#ifndef LIBTERMUX_CORE__C__ASSERT_UTILS___H
2+
#define LIBTERMUX_CORE__C__ASSERT_UTILS___H
3+
4+
#include <stdbool.h>
5+
6+
#include <termux/termux_core__c/v1/logger/Logger.h>
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
13+
14+
void assertBoolWithError(bool expected, bool actual, const char* logTag, const char *fmt, ...);
15+
void assertBool(bool expected, bool actual, const char* logTag);
16+
17+
18+
19+
void assertTrueWithError(bool actual, const char* logTag, const char *error_fmt, ...);
20+
void assertTrue(bool actual, const char* logTag);
21+
22+
23+
24+
void assertFalseWithError(bool actual, const char* logTag, const char *fmt, ...);
25+
void assertFalse(bool actual, const char* logTag);
26+
27+
28+
29+
30+
31+
void assertIntWithError(int expected, int actual, const char* logTag, const char *fmt, ...);
32+
void assertInt(int expected, int actual, const char* logTag);
33+
34+
35+
36+
37+
38+
bool stringEqualsRegardingNull(const char *expected, const char *actual);
39+
40+
void assertStringEqualsWithError(const char *expected, const char *actual,
41+
const char* logTag, const char *fmt, ...);
42+
void assertStringEquals(const char *expected, const char *actual, const char* logTag);
43+
44+
45+
46+
void assertStringNullWithError(const char *actual, const char* logTag, const char *fmt, ...);
47+
void assertStringNull(const char *actual, const char* logTag);
48+
49+
50+
51+
void assertStringNotNullWithError(const char *actual, const char* logTag, const char *fmt, ...);
52+
void assertStringNotNull(const char *actual, const char* logTag);
53+
54+
55+
56+
57+
58+
#define state__ATrue(state) \
59+
if (1) { \
60+
assertTrueWithError(state, \
61+
LOG_TAG, "%d: %s() -> (%s)", __LINE__, __FUNCTION__, #state); \
62+
} else ((void)0)
63+
64+
#define int__AEqual(expected, actual) \
65+
if (1) { \
66+
assertIntWithError(expected, actual, \
67+
LOG_TAG, "%d: %s()", __LINE__, __FUNCTION__); \
68+
} else ((void)0)
69+
70+
#define string__AEqual(expected, actual) \
71+
if (1) { \
72+
assertStringEqualsWithError(expected, actual, \
73+
LOG_TAG, "%d: %s()", __LINE__, __FUNCTION__); \
74+
} else ((void)0)
75+
76+
77+
78+
#ifdef __cplusplus
79+
}
80+
#endif
81+
82+
#endif // LIBTERMUX_CORE__C__ASSERT_UTILS___H
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef LIBTERMUX_CORE__C__DATA_UTILS___H
2+
#define LIBTERMUX_CORE__C__DATA_UTILS___H
3+
4+
#include <stdbool.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
11+
12+
/** Check if `str` starts with `prefix`. */
13+
bool stringStartsWith(const char *string, const char *prefix);
14+
15+
/** Check if `str` ends with `suffix`. */
16+
bool stringEndsWith(const char *string, const char *suffix);
17+
18+
19+
20+
21+
22+
int stringToInt(const char* string, int def, const char* logTag, const char *fmt, ...);
23+
24+
25+
26+
27+
28+
/**
29+
* Check if `string` matches the `pattern` regex.
30+
*
31+
* - https://man7.org/linux/man-pages/man0/regex.h.0p.html
32+
* - https://pubs.opengroup.org/onlinepubs/009696899/functions/regcomp.html
33+
*
34+
* @param string The string to match.
35+
* @param pattern The regex pattern to match with.
36+
* @param cflags The flags for `regcomp()` like `REG_EXTENDED`.
37+
* @return Returns `0` on match, `1` on no match and `-1` on other failures.
38+
*/
39+
int regexMatch(const char *string, const char *pattern, int cflags);
40+
41+
42+
43+
#ifdef __cplusplus
44+
}
45+
#endif
46+
47+
#endif // LIBTERMUX_CORE__C__DATA_UTILS___H

0 commit comments

Comments
 (0)