Skip to content

Commit fbbead3

Browse files
authored
Support custom temp and tests directories (#1268) (#1270)
The following user-settable values for CMake are supported: - TESTS_DIR: Tests directory, is path to unit test data when 'data' is appended. Can be used to run the unit tests on a target. - TESTS_TMPDIR: Directory for temporary files created during unit tests, system tmpdir is used if undefined. Has to be defined on systems without global temporary directory.
1 parent ee1931b commit fbbead3

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ set(TAGLIB_INSTALL_SUFFIX "" CACHE STRING
4848
"Suffix added to installed files (include directory, libraries, .pc)")
4949

5050
add_definitions(-DHAVE_CONFIG_H)
51-
set(TESTS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tests/")
51+
set(TESTS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tests/" CACHE STRING
52+
"Tests directory, is path to unit test data when 'data' is appended")
53+
set(TESTS_TMPDIR "" CACHE STRING
54+
"Directory for temporary files created during unit tests, system tmpdir is used if undefined")
5255

5356
if(CMAKE_C_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$")
5457
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")

INSTALL.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ CMakeLists.txt file.
4242
| `TAGLIB_INSTALL_SUFFIX` | Suffix added to installed libraries, includes, ... |
4343
| `ENABLE_STATIC_RUNTIME` | Link with MSVC runtime statically |
4444
| `BUILD_FRAMEWORK` | Build a macOS framework |
45+
| `TESTS_DIR` | Where to find unit test data (with data appended) |
46+
| `TESTS_TMPDIR` | Where to create temporary files in unit tests |
47+
4548

4649
If you want to install TagLib 2 alongside TagLib 1, you can use
4750
`-DTAGLIB_INSTALL_SUFFIX=-2` and make sure that `BUILD_EXAMPLES` is not `ON`
@@ -462,3 +465,54 @@ cmake --build build_mingw --config Release
462465
PATH=$PATH:$TAGLIB_PREFIX/bin
463466
build_mingw/tagreader /path/to/audio-file
464467
```
468+
469+
## Android
470+
471+
### Using vcpkg
472+
473+
The bash script below can be used to build TagLib for Android using vcpkg.
474+
It must be started in the parent folder of the taglib source folder and will
475+
build in a folder _android_build_ and install into _android_pkg_.
476+
The package and the unit tests are then transferred to an Android device
477+
and the unit tests are run on the device.
478+
479+
Note that `TESTS_TMPDIR` is set because there is no system-wide temporary folder
480+
on Android. `TESTS_DIR` is set to run the tests on the target.
481+
482+
```
483+
# You may have to adapt the NDK and vcpkg paths and the ABI/triplet.
484+
485+
export ANDROID_NDK_HOME=$HOME/Development/android-sdk/ndk/23.1.7779620
486+
export VCPKG_ROOT=$HOME/Development/vcpkg
487+
PATH=$PATH:$VCPKG_ROOT
488+
489+
# armeabi-v7a/arm-android or arm64-v8a/arm64-android or x86/x86-android or x86_64/x64-android
490+
android_abi=armeabi-v7a
491+
vcpkg_target_triplet=arm-android
492+
493+
vcpkg_toolchain_file=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
494+
android_toolchain_file=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake
495+
496+
vcpkg install --triplet $vcpkg_target_triplet utfcpp zlib cppunit
497+
498+
cmake -B android_build -S taglib \
499+
-DCMAKE_TOOLCHAIN_FILE=$vcpkg_toolchain_file \
500+
-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=$android_toolchain_file \
501+
-DVCPKG_TARGET_TRIPLET=$vcpkg_target_triplet \
502+
-DANDROID_ABI=$android_abi \
503+
-GNinja -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release \
504+
-DVISIBILITY_HIDDEN=ON -DENABLE_CCACHE=ON -DBUILD_EXAMPLES=ON -DBUILD_TESTING=ON \
505+
-DTESTS_DIR=/data/local/tmp/tests/ -DTESTS_TMPDIR=/data/local/tmp
506+
cmake --build android_build --config Release
507+
cmake --install android_build --config Release --prefix android_pkg --strip
508+
cp -a android_build/tests/test_runner android_pkg/bin/
509+
510+
if hash adb 2>/dev/null; then
511+
adb push android_pkg /data/local/tmp/
512+
adb push android_build/tests/test_runner /data/local/tmp/tests/test_runner
513+
adb push taglib/tests/data /data/local/tmp/tests/
514+
adb shell "env LD_LIBRARY_PATH=/data/local/tmp/android_pkg/lib /data/local/tmp/tests/test_runner"
515+
# You could also try an example binary:
516+
# adb shell "env LD_LIBRARY_PATH=/data/local/tmp/android_pkg/lib /data/local/tmp/android_pkg/bin/tagreader '/sdcard/Music/Some Album/A Track.mp3'"
517+
fi
518+
```

config.h.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
#cmakedefine TRACE_IN_RELEASE 1
2121

2222
#cmakedefine TESTS_DIR "@TESTS_DIR@"
23+
#cmakedefine TESTS_TMPDIR "@TESTS_TMPDIR@"
2324

2425
#endif

tests/utils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ inline string copyFile(const string &filename, const string &ext)
5454
{
5555
char testFileName[1024];
5656

57-
#ifdef _WIN32
57+
#ifdef TESTS_TMPDIR
58+
snprintf(testFileName, sizeof(testFileName), "%s/taglib-test%s", TESTS_TMPDIR, ext.c_str());
59+
#elif defined _WIN32
5860
char tempDir[MAX_PATH + 1];
5961
GetTempPathA(sizeof(tempDir), tempDir);
6062
wsprintfA(testFileName, "%s\\taglib-test%s", tempDir, ext.c_str());
6163
#else
62-
snprintf(testFileName, sizeof(testFileName), "/%s/taglib-test%s", P_tmpdir, ext.c_str());
64+
snprintf(testFileName, sizeof(testFileName), "%s/taglib-test%s", P_tmpdir, ext.c_str());
6365
#endif
6466

6567
string sourceFileName = testFilePath(filename) + ext;

0 commit comments

Comments
 (0)