From 7b648f2c924c470b342d130a32753a200b51633b Mon Sep 17 00:00:00 2001 From: Clayton Wilkinson Date: Thu, 11 Jan 2018 11:04:55 -0800 Subject: [PATCH] Updating samples to use clang. Change-Id: Ibe581bd29eca6f4e25b90da143781c290fa92129 --- samples-android/ButtonClicker/build.gradle | 9 ++-- .../CollectAllTheStarsNative/build.gradle | 5 +- samples-android/Common/JuiHelper/build.gradle | 7 +-- samples-android/Common/NDKHelper/build.gradle | 7 +-- .../NDKHelper/src/main/cpp/GLContext.cpp | 6 +-- .../NDKHelper/src/main/cpp/JNIHelper.cpp | 46 ++++++++++--------- .../Common/NDKHelper/src/main/cpp/JNIHelper.h | 7 +-- .../NDKHelper/src/main/cpp/perfMonitor.cpp | 2 +- samples-android/Common/gpg-sdk/build.gradle | 31 +++++++++++-- samples-android/Minimalist/build.gradle | 5 +- .../TbmpSkeletonNative/build.gradle | 5 +- samples-android/Teapot/build.gradle | 5 +- samples-android/build.gradle | 2 +- 13 files changed, 84 insertions(+), 53 deletions(-) diff --git a/samples-android/ButtonClicker/build.gradle b/samples-android/ButtonClicker/build.gradle index 6ed5ef6..34e85c0 100644 --- a/samples-android/ButtonClicker/build.gradle +++ b/samples-android/ButtonClicker/build.gradle @@ -32,12 +32,13 @@ android { externalNativeBuild { cmake { - cppFlags "-std=c++11 -Wall -frtti" - arguments "-DANDROID_STL=c++_static", - "-DJUI_HELPER_PATH=${project(':Common:JuiHelper').projectDir}", + cppFlags "-std=c++11 -frtti -Wall -Werror" + arguments "-DJUI_HELPER_PATH=${project(':Common:JuiHelper').projectDir}", "-DNDK_HELPER_PATH=${project(':Common:NDKHelper').projectDir}", "-DGPG_SDK_PATH=${project(':Common:gpg-sdk').projectDir}/gpg-cpp-sdk/android", - "-DTEAPOT_RENDERER_PATH=${project(':Common').projectDir}/TeapotRenderer" + "-DTEAPOT_RENDERER_PATH=${project(':Common').projectDir}/TeapotRenderer", + "-DANDROID_STL=c++_static", + "-DANDROID_TOOLCHAIN=clang" } } } diff --git a/samples-android/CollectAllTheStarsNative/build.gradle b/samples-android/CollectAllTheStarsNative/build.gradle index 6f830d7..b06290a 100644 --- a/samples-android/CollectAllTheStarsNative/build.gradle +++ b/samples-android/CollectAllTheStarsNative/build.gradle @@ -32,13 +32,14 @@ android { externalNativeBuild { cmake { - cppFlags "-std=c++11 -Wall -frtti" + cppFlags "-std=c++11 -frtti -Wall -Werror" arguments "-DJUI_HELPER_PATH=${project(':Common:JuiHelper').projectDir}" , "-DNDK_HELPER_PATH=${project(':Common:NDKHelper').projectDir}" , "-DGPG_SDK_PATH=${project(':Common:gpg-sdk').projectDir}/gpg-cpp-sdk/android", "-DJSON_PATH=${project(':Common:gpg-sdk').projectDir}/../external/jsoncpp/", "-DTEAPOT_RENDERER_PATH=${project(':Common').projectDir}/TeapotRenderer" , - "-DANDROID_STL=c++_static" + "-DANDROID_STL=c++_static", + "-DANDROID_TOOLCHAIN=clang" } } diff --git a/samples-android/Common/JuiHelper/build.gradle b/samples-android/Common/JuiHelper/build.gradle index 0cc7bc4..5d27e96 100644 --- a/samples-android/Common/JuiHelper/build.gradle +++ b/samples-android/Common/JuiHelper/build.gradle @@ -24,10 +24,11 @@ android { externalNativeBuild { cmake { - cppFlags "-std=c++11 -Wall -frtti" + cppFlags "-std=c++11 -frtti -Wall -Werror" targets "juihelper" - arguments "-DANDROID_STL=c++_static", - "-DNDK_HELPER_PATH=${project(':Common:NDKHelper').projectDir}" + arguments "-DNDK_HELPER_PATH=${project(':Common:NDKHelper').projectDir}", + "-DANDROID_STL=c++_static", + "-DANDROID_TOOLCHAIN=clang" } } } diff --git a/samples-android/Common/NDKHelper/build.gradle b/samples-android/Common/NDKHelper/build.gradle index 6349048..72a846d 100644 --- a/samples-android/Common/NDKHelper/build.gradle +++ b/samples-android/Common/NDKHelper/build.gradle @@ -24,10 +24,11 @@ android { externalNativeBuild { cmake { - cppFlags "-std=c++11 -Wall -frtti" + cppFlags "-std=c++11 -frtti -Wall -Werror" targets "ndkhelper" - arguments "-DANDROID_STL=c++_static", - "-DNDK_HELPER_PATH=${project(':Common:JuiHelper').projectDir}" + arguments "-DNDK_HELPER_PATH=${project(':Common:JuiHelper').projectDir}", + "-DANDROID_STL=c++_static", + "-DANDROID_TOOLCHAIN=clang" } } } diff --git a/samples-android/Common/NDKHelper/src/main/cpp/GLContext.cpp b/samples-android/Common/NDKHelper/src/main/cpp/GLContext.cpp index d67b06e..753ca4d 100644 --- a/samples-android/Common/NDKHelper/src/main/cpp/GLContext.cpp +++ b/samples-android/Common/NDKHelper/src/main/cpp/GLContext.cpp @@ -49,7 +49,7 @@ void GLContext::InitGLES() { // //Initialize OpenGL ES 3 if available // - const char *versionStr = (const char *)glGetString(GL_VERSION); + const char *versionStr = reinterpret_cast(glGetString(GL_VERSION)); if (strstr(versionStr, "OpenGL ES 3.") && gl3stubInit()) { es3_supported_ = true; gl_version_ = 3.0f; @@ -92,7 +92,7 @@ bool GLContext::InitEGLSurface() { * Below, we select an EGLConfig with at least 8 bits per color * component compatible with on-screen windows */ - const EGLint attribs[] = { EGL_RENDERABLE_TYPE, + const EGLint attribs0[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, //Request opengl ES2.0 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, @@ -101,7 +101,7 @@ bool GLContext::InitEGLSurface() { depth_size_ = 24; EGLint num_configs; - eglChooseConfig(display_, attribs, &config_, 1, &num_configs); + eglChooseConfig(display_, attribs0, &config_, 1, &num_configs); if (msaa_size_ > 1 && !num_configs) { LOGW("No EGL config with MSAA"); diff --git a/samples-android/Common/NDKHelper/src/main/cpp/JNIHelper.cpp b/samples-android/Common/NDKHelper/src/main/cpp/JNIHelper.cpp index d247d8f..5894c24 100644 --- a/samples-android/Common/NDKHelper/src/main/cpp/JNIHelper.cpp +++ b/samples-android/Common/NDKHelper/src/main/cpp/JNIHelper.cpp @@ -34,8 +34,8 @@ namespace ndk_helper { * Singleton */ JNIHelper *JNIHelper::GetInstance() { - static JNIHelper helper; - return &helper; + static JNIHelper* pHelper = new JNIHelper(); + return pHelper; } /* @@ -73,14 +73,14 @@ void JNIHelper::Init(ANativeActivity *activity, const char *helper_class_name) { jmethodID midGetPackageName = env->GetMethodID( android_content_Context, "getPackageName", "()Ljava/lang/String;"); - jstring packageName = (jstring) - env->CallObjectMethod(helper.activity_->clazz, midGetPackageName); + jstring packageName = static_cast( + env->CallObjectMethod(helper.activity_->clazz, midGetPackageName)); const char *appname = env->GetStringUTFChars(packageName, NULL); helper.app_bunlde_name_ = std::string(appname); //Instantiate JNIHelper class jclass cls = helper.RetrieveClass(env, helper_class_name); - helper.jni_helper_java_class_ = (jclass) env->NewGlobalRef(cls); + helper.jni_helper_java_class_ = static_cast(env->NewGlobalRef(cls)); jmethodID constructor = env->GetMethodID(helper.jni_helper_java_class_, "", @@ -91,8 +91,8 @@ void JNIHelper::Init(ANativeActivity *activity, const char *helper_class_name) { helper.jni_helper_java_ref_ = env->NewGlobalRef(helper.jni_helper_java_ref_); //Get app label - jstring labelName = (jstring) - helper.CallObjectMethod("getApplicationName", "()Ljava/lang/String;"); + jstring labelName = static_cast( + helper.CallObjectMethod("getApplicationName", "()Ljava/lang/String;")); const char *label = env->GetStringUTFChars(labelName, NULL); helper.app_label_ = std::string(label); @@ -158,9 +158,9 @@ bool JNIHelper::ReadFile(const char *fileName, if (f) { LOGI("reading:%s", s.c_str()); f.seekg(0, std::ifstream::end); - int32_t fileSize = f.tellg(); + long long int fileSize = f.tellg(); f.seekg(0, std::ifstream::beg); - buffer_ref->reserve(fileSize); + buffer_ref->reserve(static_cast(fileSize)); buffer_ref->assign(std::istreambuf_iterator(f), std::istreambuf_iterator()); f.close(); @@ -173,8 +173,8 @@ bool JNIHelper::ReadFile(const char *fileName, if (!assetFile) { return false; } - uint8_t *data = (uint8_t *)AAsset_getBuffer(assetFile); - int32_t size = AAsset_getLength(assetFile); + const uint8_t *data = static_cast(AAsset_getBuffer(assetFile)); + size_t size = static_cast(AAsset_getLength(assetFile)); if (data == NULL) { AAsset_close(assetFile); @@ -250,11 +250,11 @@ uint32_t JNIHelper::LoadTexture(const char *file_name, int32_t *outWidth, int32_t height = env->GetIntField(out, fidHeight); if (!ret) { glDeleteTextures(1, &tex); - tex = -1; + tex = 0xffff; LOGI("Texture load failed %s", file_name); } LOGI("Loaded texture original size:%dx%d alpha:%d", width, height, - (int32_t) alpha); + static_cast(alpha)); if (outWidth != NULL) { *outWidth = width; } @@ -287,16 +287,16 @@ std::string JNIHelper::ConvertString(const char *str, const char *encode) { JNIEnv *env = AttachCurrentThread(); env->PushLocalFrame(16); - int32_t iLength = strlen((const char *)str); + jsize iLength = static_cast(strlen(str)); jbyteArray array = env->NewByteArray(iLength); - env->SetByteArrayRegion(array, 0, iLength, (const signed char *)str); + env->SetByteArrayRegion(array, 0, iLength, reinterpret_cast(str)); jstring strEncode = env->NewStringUTF(encode); jclass cls = env->FindClass("java/lang/String"); jmethodID ctor = env->GetMethodID(cls, "", "([BLjava/lang/String;)V"); - jstring object = (jstring) env->NewObject(cls, ctor, array, strEncode); + jstring object = static_cast(env->NewObject(cls, ctor, array, strEncode)); const char *cparam = env->GetStringUTFChars(object, NULL); @@ -332,8 +332,8 @@ std::string JNIHelper::GetStringResource(const std::string& resourceName) JNIEnv *env = AttachCurrentThread(); jstring name = env->NewStringUTF(resourceName.c_str()); - jstring ret = (jstring) - CallObjectMethod("getStringResource", "(Ljava/lang/String;)Ljava/lang/String;", name); + jstring ret = static_cast( + CallObjectMethod("getStringResource", "(Ljava/lang/String;)Ljava/lang/String;", name)); const char *resource = env->GetStringUTFChars(ret, NULL); std::string s = std::string(resource); @@ -389,7 +389,7 @@ jclass JNIHelper::RetrieveClass(JNIEnv *jni, const char *class_name) { jstring str_class_name = jni->NewStringUTF(class_name); jclass class_retrieved = - (jclass) jni->CallObjectMethod(cls, find_class, str_class_name); + static_cast(jni->CallObjectMethod(cls, find_class, str_class_name)); jni->DeleteLocalRef(str_class_name); jni->DeleteLocalRef(activity_class); jni->DeleteLocalRef(class_loader); @@ -411,7 +411,7 @@ jstring JNIHelper::GetExternalFilesDirJString(JNIEnv *env) { jclass cls_File = env->FindClass("java/io/File"); jmethodID mid_getPath = env->GetMethodID(cls_File, "getPath", "()Ljava/lang/String;"); - jstring obj_Path = (jstring) env->CallObjectMethod(obj_File, mid_getPath); + jstring obj_Path = static_cast(env->CallObjectMethod(obj_File, mid_getPath)); return obj_Path; } @@ -622,7 +622,7 @@ void JNIHelper::RunOnUiThread(std::function callback) { // Allocate temporary function object to be passed around std::function *pCallback = new std::function(callback); - env->CallVoidMethod(jni_helper_java_ref_, mid, (int64_t) pCallback); + env->CallVoidMethod(jni_helper_java_ref_, mid, reinterpret_cast(pCallback)); } // This JNI function is invoked from UIThread asynchronously @@ -630,7 +630,9 @@ extern "C" { JNIEXPORT void Java_com_sample_helper_NDKHelper_RunOnUiThreadHandler(JNIEnv *env, jobject thiz, int64_t pointer) { - std::function *pCallback = (std::function *)pointer; +#pragma unused (env) +#pragma unused (thiz) + std::function *pCallback = reinterpret_cast< std::function *>(pointer); (*pCallback)(); // Deleting temporary object diff --git a/samples-android/Common/NDKHelper/src/main/cpp/JNIHelper.h b/samples-android/Common/NDKHelper/src/main/cpp/JNIHelper.h index abac136..049d855 100644 --- a/samples-android/Common/NDKHelper/src/main/cpp/JNIHelper.h +++ b/samples-android/Common/NDKHelper/src/main/cpp/JNIHelper.h @@ -218,10 +218,11 @@ class JNIHelper { */ JNIEnv *AttachCurrentThread() { JNIEnv *env; - if (activity_->vm->GetEnv((void **)&env, JNI_VERSION_1_4) == JNI_OK) + if (activity_->vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_4) == JNI_OK) return env; activity_->vm->AttachCurrentThread(&env, NULL); - pthread_key_create((int32_t *)activity_, DetachCurrentThreadDtor); + pthread_key_create( + reinterpret_cast(activity_), DetachCurrentThreadDtor); return env; } @@ -285,7 +286,7 @@ class JNIHelper { */ static void DetachCurrentThreadDtor(void *p) { LOGI("detached current thread"); - ANativeActivity *activity = (ANativeActivity *)p; + ANativeActivity *activity = reinterpret_cast(p); activity->vm->DetachCurrentThread(); } diff --git a/samples-android/Common/NDKHelper/src/main/cpp/perfMonitor.cpp b/samples-android/Common/NDKHelper/src/main/cpp/perfMonitor.cpp index 2ee2b39..b778523 100644 --- a/samples-android/Common/NDKHelper/src/main/cpp/perfMonitor.cpp +++ b/samples-android/Common/NDKHelper/src/main/cpp/perfMonitor.cpp @@ -19,7 +19,7 @@ namespace ndk_helper { PerfMonitor::PerfMonitor() - : current_FPS_(0), tv_last_sec_(0), last_tick_(0.f), tickindex_(0), + : current_FPS_(0), tv_last_sec_(0), last_tick_(0), tickindex_(0), ticksum_(0) { for (int32_t i = 0; i < NUM_SAMPLES; ++i) ticklist_[i] = 0; diff --git a/samples-android/Common/gpg-sdk/build.gradle b/samples-android/Common/gpg-sdk/build.gradle index 3010323..e4195a7 100644 --- a/samples-android/Common/gpg-sdk/build.gradle +++ b/samples-android/Common/gpg-sdk/build.gradle @@ -1,9 +1,26 @@ +/* Copyright (C) 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /* sub module to download the Google Play games */ project.ext { if (!project.hasProperty('gpg_sdk_link')) { - gpg_sdk_link = 'https://developers.google.com/games/services/downloads/gpg-cpp-sdk.v3.0.zip' + gpg_sdk_link = System.getenv("GPG_SDK_LINK") + if (gpg_sdk_link == null || gpg_sdk_link.isEmpty()) { + gpg_sdk_link = 'https://developers.google.com/games/services/downloads/gpg-cpp-sdk.v3.0.zip' + } } } task download_and_stage_gpg_sdk(dependsOn:'unzip_gpg_sdk') { @@ -24,19 +41,23 @@ task fetch_gpg_cpp_sdk () { fetch_gpg_cpp_sdk.description = "Download the gpg sdk from the specified location" task unzip_gpg_sdk() { + def dest = file( "${project.projectDir}/gpg-cpp-sdk") doFirst { - if (!file('gpg-cpp-sdk').exists()) { + if (!dest.exists()) { copy { from(zipTree('gpg_cpp_sdk.zip')) { exclude 'gpg-cpp-sdk/ios/**/*' + include 'gpg-cpp-sdk/android/**/*' } into { project.projectDir } } + } else { + println "Skipping sdk unzipping, ${dest.absolutePath} exists" } } - outputs.dir('gpg-cpp-sdk/android') - outputs.upToDateWhen {file('gpg-cpp-sdk/android').exists()} + outputs.upToDateWhen {file("${dest.absolutePath}/android").exists() && + file('gpg-cpp-sdk/android/lib/c++/arm64-v8a/libgpg.a').exists()} description = "Unzips the GPG SDK into the correct dir for NDK" dependsOn fetch_gpg_cpp_sdk } @@ -51,4 +72,4 @@ task clean() { } } -defaultTasks = ['download_and_stage_gpg_sdk'] +defaultTasks = ['unzip_gpg_sdk'] diff --git a/samples-android/Minimalist/build.gradle b/samples-android/Minimalist/build.gradle index 6a0b29d..46d3616 100644 --- a/samples-android/Minimalist/build.gradle +++ b/samples-android/Minimalist/build.gradle @@ -31,9 +31,10 @@ android { externalNativeBuild { cmake { - cppFlags "-std=c++11 -Wall -frtti" + cppFlags "-std=c++11 -frtti -Wall -Werror" arguments "-DGPG_SDK_PATH=${project(':Common:gpg-sdk').projectDir}/gpg-cpp-sdk/android", - "-DANDROID_STL=c++_static" + "-DANDROID_STL=c++_static", + "-DANDROID_TOOLCHAIN=clang" } } } diff --git a/samples-android/TbmpSkeletonNative/build.gradle b/samples-android/TbmpSkeletonNative/build.gradle index 0d46d7a..df1d311 100644 --- a/samples-android/TbmpSkeletonNative/build.gradle +++ b/samples-android/TbmpSkeletonNative/build.gradle @@ -31,13 +31,14 @@ android { externalNativeBuild { cmake { - cppFlags "-std=c++11 -Wall -frtti" + cppFlags "-std=c++11 -frtti -Wall -Werror" arguments "-DJUI_HELPER_PATH=${project(':Common:JuiHelper').projectDir}" , "-DNDK_HELPER_PATH=${project(':Common:NDKHelper').projectDir}" , "-DGPG_SDK_PATH=${project(':Common:gpg-sdk').projectDir}/gpg-cpp-sdk/android", "-DJSON_PATH=${project(':Common:gpg-sdk').projectDir}/../external/jsoncpp/", "-DTEAPOT_RENDERER_PATH=${project(':Common').projectDir}/TeapotRenderer" , - "-DANDROID_STL=c++_static" + "-DANDROID_STL=c++_static", + "-DANDROID_TOOLCHAIN=clang" } } diff --git a/samples-android/Teapot/build.gradle b/samples-android/Teapot/build.gradle index 94a930b..e24a2a3 100644 --- a/samples-android/Teapot/build.gradle +++ b/samples-android/Teapot/build.gradle @@ -31,12 +31,13 @@ apply plugin: 'com.android.application' externalNativeBuild { cmake { - cppFlags "-std=c++11 -Wall -frtti" + cppFlags "-std=c++11 -frtti -Wall -Werror" arguments "-DJUI_HELPER_PATH=${project(':Common:JuiHelper').projectDir}" , "-DNDK_HELPER_PATH=${project(':Common:NDKHelper').projectDir}" , "-DGPG_SDK_PATH=${project(':Common:gpg-sdk').projectDir}/gpg-cpp-sdk/android", "-DTEAPOT_RENDERER_PATH=${project(':Common').projectDir}/TeapotRenderer" , - "-DANDROID_STL=c++_static" + "-DANDROID_STL=c++_static", + "-DANDROID_TOOLCHAIN=clang" } } diff --git a/samples-android/build.gradle b/samples-android/build.gradle index 9860392..f788858 100644 --- a/samples-android/build.gradle +++ b/samples-android/build.gradle @@ -37,7 +37,7 @@ allprojects { // As the plugin has created the build tasks, add the gpg-sdk task to // the task dependencies so it gets done before compiling tasks.whenTaskAdded { - task->project(':Common:gpg-sdk').defaultTasks.each { + task-> if (!task.name.endsWith("clean")) project(':Common:gpg-sdk').defaultTasks.each { t->task.dependsOn project(':Common:gpg-sdk').tasks[t] } }