microsoft / cpp_client_telemetry

1DS C++ SDK
Apache License 2.0
87 stars 49 forks source link

How to compile the SDK with CS 4.0 in Android in command line? #1136

Open TedaLIEz opened 1 year ago

TedaLIEz commented 1 year ago

Please:

I want to compile this SDK with CS 4.0 support and use it as an AAR in Android. I read through the source code and I found one marco "HAVE_CS4", I tried to compile the SDK based on v3.7.62.1 with flag -DCMAKE_CXX_FLAGS="-DHAVE_CS4=1"

maesdk/build.gradle:

if(!ext.has("build_cpp_client") || ext.build_cpp_client) {
            externalNativeBuild {
                cmake {
                    // Passes optional arguments to CMake.
                    arguments "-DCMAKE_CXX_FLAGS=\"-DHAVE_CS4=1\"", "-DANDROID_STL=c++_shared", "-DBUILD_SHARED_LIBS=1", "-DUSE_ROOM=1", "-DCMAKE_SHARED_LINKER_FLAGS=${project.findProperty("CMAKE_SHARED_LINKER_FLAGS") ?: ''}"
                }
            }
        }

However, this requires us to make changes in your gradle file, and we need to commit this change to repo to track the history. Besides, it makes it hard for us to set up this repo as a git submodule since we have to push the change to remote for submodule.

Can we enable HAVE_CS4 during the build process? For example, we can set this flag in build pipeline like: `./gradlew build -PCXX_FLAG=HAVE_CS4".

lalitb commented 1 year ago

I thought that setting environment variable CXXFLAGS before invoking gradle/gradlew should work, and cmake should pick up the compilation flags automatically from this environment variable. But seems this is not working as you described.

In that case, modifying build.gradle is a fair ask. Something like this should work

if(!ext.has("build_cpp_client") || ext.build_cpp_client) {
            externalNativeBuild {
                cmake {
                    // Passes optional arguments to CMake.
                    arguments "-DCMAKE_CXX_FLAGS=${project.findProperty("CMAKE_CXX_FLAGS") ?: ''}", "-DANDROID_STL=c++_shared", "-DBUILD_SHARED_LIBS=1", "-DUSE_ROOM=1", "-DCMAKE_SHARED_LINKER_FLAGS=${project.findProperty("CMAKE_SHARED_LINKER_FLAGS") ?: ''}"
                }
            }
        }

And then $ ./gradlew build -PCMAKE_CXX_FLAGS="-DHAVE_CS4=1"

@TedaLIEz - As you are already trying this out, would you like to contribute above changes in this repo. We can help in review and merge.

tagging @anod as he knows these scripts better :)

anod commented 1 year ago

I agree with Lalit that passing parameter to build is more explicit, in CMD, notice there are different 2 properties types:

-D, --system-prop                 Set system property of the JVM (e.g. -Dmyprop=myvalue).
-P, --project-prop                 Set project property for the build script (e.g. -Pmyprop=myvalue).

You can also fallback to env with something like: -DMAKE_CXX_FLAGS=${project.findProperty("MAKE_CXX_FLAGS") ?: System.getenv("MAKE_CXX_FLAGS")}

For some flags we use explicit parameter in cases where it might be use for cmake configuration (like USE_ROOM), so as an option is to define similar USE_CS4