Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement USE_OS_TZDB for Android #842

Merged
merged 1 commit into from
Sep 29, 2024

Conversation

basilgello
Copy link
Contributor

This patch implements native tzdata parser for Android. This allows developers to re-use built-in Android tzdata without the need to ship and update IANA TZ data.

@basilgello
Copy link
Contributor Author

@HowardHinnant I fixed private problem :)

@@ -139,6 +139,9 @@ namespace date

enum class choose {earliest, latest};

struct tzdb;
static std::unique_ptr<tzdb> init_tzdb();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On macOS with -Wall:

../date/include/date/tz.h:143:30: warning: unused function 'init_tzdb' [-Wunused-function]
  143 | static std::unique_ptr<tzdb> init_tzdb();
      |                              ^~~~~~~~~
1 warning generated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is worth guarding with Android ifdefs here and leave the original declaration guarded by non-Android ifdefs. Otherwise on Android, the first declaration of function is non-static but then a static friend function is expected, throwing a hard error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole idea was to allow init_tzdb accessing time_zone::parse_from_android_tzdata but keep that one private for all other possible users.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to test a fix to this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Pushed the fix. Now it builds against Clang 18.1.0 in Android NDK r27b.

-Wall on Android:

[  0%] Building CXX object CMakeFiles/date-tz.dir/src/tz.cpp.o
/tmp/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi21 --sysroot=/tmp/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/sysroot -DAUTO_DOWNLOAD=0 -DHAS_REMOTE_API=0 -DHAS_STRING_VIEW=1 -DINSTALL=. -DONLY_C_LOCALE=0 -DUSE_OS_TZDB=1 -I/tmp/date/include -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security  -Wall -std=gnu++17 -fPIC -pthread -MD -MT CMakeFiles/date-tz.dir/src/tz.cpp.o -MF CMakeFiles/date-tz.dir/src/tz.cpp.o.d -o CMakeFiles/date-tz.dir/src/tz.cpp.o -c /tmp/date/src/tz.cpp
/tmp/date/src/tz.cpp:195:23: warning: unused variable 'folder_delimiter' [-Wunused-const-variable]
  195 | static CONSTDATA char folder_delimiter = '/';
      |                       ^~~~~~~~~~~~~~~~
/tmp/date/src/tz.cpp:655:1: warning: unused function 'parse_month' [-Wunused-function]
  655 | parse_month(std::istream& in)
      | ^~~~~~~~~~~
/tmp/date/src/tz.cpp:2896:1: warning: unused function 'get_version' [-Wunused-function]
 2896 | get_version()
      | ^~~~~~~~~~~
3 warnings generated.

Should I guard these unused-s too?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but could you put that in a separate PR since I've already merged this one? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will do!

@farfromrefug
Copy link

farfromrefug commented Nov 4, 2024

I tried to use latest to get this but it wont build on android. I get multiple errors while trying to build with -DUSE_OS_TZDB=1

  • tz.cpp:3039:18: error: 'parse_from_android_tzdata' is a private member of 'date::time_zone' timezone.parse_from_android_tzdata(in, hdr.data_offset + index_entry.start
  • init_tzdb is not defined because of the #if !defined(ANDROID) line 558, while it is called by create_tzdb before the method is defined line 3010.

Am i doing something wrong?

EDIT: btw should it work on all android devices (reading from /apex/com.android.tzdata/etc/tz or /system/usr/share/zoneinfo) ? or will it only work on rooted device?

@basilgello
Copy link
Contributor Author

It is supposed to work on any device, not just rooted.

And I have checked (and using it myself) against recent NDKs (25, 26, 27). Which NDK are you trying to build it with?

@farfromrefug
Copy link

farfromrefug commented Nov 5, 2024

@basilgello i am building with NDK 25 right now:

/home/mguillon/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=x86_64-none-linux-android21 --sysroot=/home/mguillon/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot -DBOOST_ALL_NO_LIB -I/home/mguillon/dev/carto/mobile-sdk/scripts/build/../../libs-external/boost -I/home/mguillon/dev/carto/mobile-sdk/scripts/build/../../libs-external/boost/libs/any/include -I/home/mguillon/dev/carto/mobile-sdk/libs-external/date/date/include -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security   -std=c++17 -ftemplate-depth=1024 -fexceptions -frtti -fvisibility=hidden -fvisibility-inlines-hidden -fno-limit-debug-info  -fPIC   -D_CARTO_SEARCH_SUPPORT -D_CARTO_GEOCODING_SUPPORT -D_CARTO_ROUTING_SUPPORT -D_CARTO_OFFLINE_SUPPORT -D_CARTO_PACKAGEMANAGER_SUPPORT -D_CARTO_EDITABLE_SUPPORT -D_CARTO_WKBT_SUPPORT -D_CARTO_VALHALLA_ROUTING_SUPPORT -D_CARTO_SEARCH_SUPPORT -D_CARTO_GEOCODING_SUPPORT -D_CARTO_ROUTING_SUPPORT -D_CARTO_OFFLINE_SUPPORT -D_CARTO_PACKAGEMANAGER_SUPPORT -D_CARTO_EDITABLE_SUPPORT -D_CARTO_WKBT_SUPPORT -DUSE_OS_TZDB=1 -MD -MT date/CMakeFiles/date.dir/date/src/tz.cpp.o -MF date/CMakeFiles/date.dir/date/src/tz.cpp.o.d -o date/CMakeFiles/date.dir/date/src/tz.cpp.o -c /home/mguillon/dev/carto/mobile-sdk/libs-external/date/date/src/tz.cpp

/home/mguillon/dev/carto/mobile-sdk/libs-external/date/date/src/tz.cpp:608:59: error: use of undeclared identifier 'init_tzdb'; did you mean 'get_tzdb'?
        tzdb_list::undocumented_helper::push_front(tz_db, init_tzdb().release());
                                                          ^~~~~~~~~
                                                          get_tzdb
/home/mguillon/dev/carto/mobile-sdk/libs-external/date/date/include/date/tz.h:1221:22: note: 'get_tzdb' declared here
DATE_API const tzdb& get_tzdb();
                     ^

And i checked i have the latest code. From my gradle file i call cmake with '-DUSE_OS_TZDB=1'
Would you have an idea on what the issue might be?

EDIT: also tried with ndk 27.0.11902837 and got the same error
EDIT: do i need to set BUILD_TZ_LIB ? Not sure what it is for.

@basilgello
Copy link
Contributor Author

Yes you need -DBUILD_TZ_LIB=ON because src/* are included into build process with it. I think we can add the dependency trigger for USE_OS_TZDB to turn on BUILD_TZ_LIB ON.

@farfromrefug
Copy link

@basilgello thanks i actually in my case i dont use data CMakeLists.txt so i should not need BUILD_TZ_LIB.
I solved by modifying the code that way Akylas@b5d13dd.
Dont really understand how it can work for you. Especially the private method error

@basilgello
Copy link
Contributor Author

It is a function init_tzdb that on Android is made a friend function to a time_zone, making it possible to access its private methods.

@basilgello
Copy link
Contributor Author

You can check if CMake build passes different set of flags to clang++ than you specified. Just make VERBOSE=1 and note the clang++ invocation command line.

@farfromrefug
Copy link

@basilgello ok i see something must be wrong with my setup. Will check thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants