Skip to content

Commit 7b648f2

Browse files
committed
Updating samples to use clang.
Change-Id: Ibe581bd29eca6f4e25b90da143781c290fa92129
1 parent 1e1f1e7 commit 7b648f2

File tree

13 files changed

+84
-53
lines changed

13 files changed

+84
-53
lines changed

samples-android/ButtonClicker/build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ android {
3232

3333
externalNativeBuild {
3434
cmake {
35-
cppFlags "-std=c++11 -Wall -frtti"
36-
arguments "-DANDROID_STL=c++_static",
37-
"-DJUI_HELPER_PATH=${project(':Common:JuiHelper').projectDir}",
35+
cppFlags "-std=c++11 -frtti -Wall -Werror"
36+
arguments "-DJUI_HELPER_PATH=${project(':Common:JuiHelper').projectDir}",
3837
"-DNDK_HELPER_PATH=${project(':Common:NDKHelper').projectDir}",
3938
"-DGPG_SDK_PATH=${project(':Common:gpg-sdk').projectDir}/gpg-cpp-sdk/android",
40-
"-DTEAPOT_RENDERER_PATH=${project(':Common').projectDir}/TeapotRenderer"
39+
"-DTEAPOT_RENDERER_PATH=${project(':Common').projectDir}/TeapotRenderer",
40+
"-DANDROID_STL=c++_static",
41+
"-DANDROID_TOOLCHAIN=clang"
4142
}
4243
}
4344
}

samples-android/CollectAllTheStarsNative/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ android {
3232

3333
externalNativeBuild {
3434
cmake {
35-
cppFlags "-std=c++11 -Wall -frtti"
35+
cppFlags "-std=c++11 -frtti -Wall -Werror"
3636
arguments "-DJUI_HELPER_PATH=${project(':Common:JuiHelper').projectDir}" ,
3737
"-DNDK_HELPER_PATH=${project(':Common:NDKHelper').projectDir}" ,
3838
"-DGPG_SDK_PATH=${project(':Common:gpg-sdk').projectDir}/gpg-cpp-sdk/android",
3939
"-DJSON_PATH=${project(':Common:gpg-sdk').projectDir}/../external/jsoncpp/",
4040
"-DTEAPOT_RENDERER_PATH=${project(':Common').projectDir}/TeapotRenderer" ,
41-
"-DANDROID_STL=c++_static"
41+
"-DANDROID_STL=c++_static",
42+
"-DANDROID_TOOLCHAIN=clang"
4243
}
4344

4445
}

samples-android/Common/JuiHelper/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ android {
2424

2525
externalNativeBuild {
2626
cmake {
27-
cppFlags "-std=c++11 -Wall -frtti"
27+
cppFlags "-std=c++11 -frtti -Wall -Werror"
2828
targets "juihelper"
29-
arguments "-DANDROID_STL=c++_static",
30-
"-DNDK_HELPER_PATH=${project(':Common:NDKHelper').projectDir}"
29+
arguments "-DNDK_HELPER_PATH=${project(':Common:NDKHelper').projectDir}",
30+
"-DANDROID_STL=c++_static",
31+
"-DANDROID_TOOLCHAIN=clang"
3132
}
3233
}
3334
}

samples-android/Common/NDKHelper/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ android {
2424

2525
externalNativeBuild {
2626
cmake {
27-
cppFlags "-std=c++11 -Wall -frtti"
27+
cppFlags "-std=c++11 -frtti -Wall -Werror"
2828
targets "ndkhelper"
29-
arguments "-DANDROID_STL=c++_static",
30-
"-DNDK_HELPER_PATH=${project(':Common:JuiHelper').projectDir}"
29+
arguments "-DNDK_HELPER_PATH=${project(':Common:JuiHelper').projectDir}",
30+
"-DANDROID_STL=c++_static",
31+
"-DANDROID_TOOLCHAIN=clang"
3132
}
3233
}
3334
}

samples-android/Common/NDKHelper/src/main/cpp/GLContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void GLContext::InitGLES() {
4949
//
5050
//Initialize OpenGL ES 3 if available
5151
//
52-
const char *versionStr = (const char *)glGetString(GL_VERSION);
52+
const char *versionStr = reinterpret_cast<const char*>(glGetString(GL_VERSION));
5353
if (strstr(versionStr, "OpenGL ES 3.") && gl3stubInit()) {
5454
es3_supported_ = true;
5555
gl_version_ = 3.0f;
@@ -92,7 +92,7 @@ bool GLContext::InitEGLSurface() {
9292
* Below, we select an EGLConfig with at least 8 bits per color
9393
* component compatible with on-screen windows
9494
*/
95-
const EGLint attribs[] = { EGL_RENDERABLE_TYPE,
95+
const EGLint attribs0[] = { EGL_RENDERABLE_TYPE,
9696
EGL_OPENGL_ES2_BIT, //Request opengl ES2.0
9797
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8,
9898
EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE,
@@ -101,7 +101,7 @@ bool GLContext::InitEGLSurface() {
101101
depth_size_ = 24;
102102

103103
EGLint num_configs;
104-
eglChooseConfig(display_, attribs, &config_, 1, &num_configs);
104+
eglChooseConfig(display_, attribs0, &config_, 1, &num_configs);
105105

106106
if (msaa_size_ > 1 && !num_configs) {
107107
LOGW("No EGL config with MSAA");

samples-android/Common/NDKHelper/src/main/cpp/JNIHelper.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ namespace ndk_helper {
3434
* Singleton
3535
*/
3636
JNIHelper *JNIHelper::GetInstance() {
37-
static JNIHelper helper;
38-
return &helper;
37+
static JNIHelper* pHelper = new JNIHelper();
38+
return pHelper;
3939
}
4040

4141
/*
@@ -73,14 +73,14 @@ void JNIHelper::Init(ANativeActivity *activity, const char *helper_class_name) {
7373
jmethodID midGetPackageName = env->GetMethodID(
7474
android_content_Context, "getPackageName", "()Ljava/lang/String;");
7575

76-
jstring packageName = (jstring)
77-
env->CallObjectMethod(helper.activity_->clazz, midGetPackageName);
76+
jstring packageName = static_cast<jstring>(
77+
env->CallObjectMethod(helper.activity_->clazz, midGetPackageName));
7878
const char *appname = env->GetStringUTFChars(packageName, NULL);
7979
helper.app_bunlde_name_ = std::string(appname);
8080

8181
//Instantiate JNIHelper class
8282
jclass cls = helper.RetrieveClass(env, helper_class_name);
83-
helper.jni_helper_java_class_ = (jclass) env->NewGlobalRef(cls);
83+
helper.jni_helper_java_class_ = static_cast<jclass>(env->NewGlobalRef(cls));
8484

8585
jmethodID constructor =
8686
env->GetMethodID(helper.jni_helper_java_class_, "<init>",
@@ -91,8 +91,8 @@ void JNIHelper::Init(ANativeActivity *activity, const char *helper_class_name) {
9191
helper.jni_helper_java_ref_ = env->NewGlobalRef(helper.jni_helper_java_ref_);
9292

9393
//Get app label
94-
jstring labelName = (jstring)
95-
helper.CallObjectMethod("getApplicationName", "()Ljava/lang/String;");
94+
jstring labelName = static_cast<jstring>(
95+
helper.CallObjectMethod("getApplicationName", "()Ljava/lang/String;"));
9696
const char *label = env->GetStringUTFChars(labelName, NULL);
9797
helper.app_label_ = std::string(label);
9898

@@ -158,9 +158,9 @@ bool JNIHelper::ReadFile(const char *fileName,
158158
if (f) {
159159
LOGI("reading:%s", s.c_str());
160160
f.seekg(0, std::ifstream::end);
161-
int32_t fileSize = f.tellg();
161+
long long int fileSize = f.tellg();
162162
f.seekg(0, std::ifstream::beg);
163-
buffer_ref->reserve(fileSize);
163+
buffer_ref->reserve(static_cast<unsigned long>(fileSize));
164164
buffer_ref->assign(std::istreambuf_iterator<char>(f),
165165
std::istreambuf_iterator<char>());
166166
f.close();
@@ -173,8 +173,8 @@ bool JNIHelper::ReadFile(const char *fileName,
173173
if (!assetFile) {
174174
return false;
175175
}
176-
uint8_t *data = (uint8_t *)AAsset_getBuffer(assetFile);
177-
int32_t size = AAsset_getLength(assetFile);
176+
const uint8_t *data = static_cast<const uint8_t*>(AAsset_getBuffer(assetFile));
177+
size_t size = static_cast<size_t>(AAsset_getLength(assetFile));
178178
if (data == NULL) {
179179
AAsset_close(assetFile);
180180

@@ -250,11 +250,11 @@ uint32_t JNIHelper::LoadTexture(const char *file_name, int32_t *outWidth,
250250
int32_t height = env->GetIntField(out, fidHeight);
251251
if (!ret) {
252252
glDeleteTextures(1, &tex);
253-
tex = -1;
253+
tex = 0xffff;
254254
LOGI("Texture load failed %s", file_name);
255255
}
256256
LOGI("Loaded texture original size:%dx%d alpha:%d", width, height,
257-
(int32_t) alpha);
257+
static_cast<int32_t>(alpha));
258258
if (outWidth != NULL) {
259259
*outWidth = width;
260260
}
@@ -287,16 +287,16 @@ std::string JNIHelper::ConvertString(const char *str, const char *encode) {
287287
JNIEnv *env = AttachCurrentThread();
288288
env->PushLocalFrame(16);
289289

290-
int32_t iLength = strlen((const char *)str);
290+
jsize iLength = static_cast<jsize>(strlen(str));
291291

292292
jbyteArray array = env->NewByteArray(iLength);
293-
env->SetByteArrayRegion(array, 0, iLength, (const signed char *)str);
293+
env->SetByteArrayRegion(array, 0, iLength, reinterpret_cast<const jbyte*>(str));
294294

295295
jstring strEncode = env->NewStringUTF(encode);
296296

297297
jclass cls = env->FindClass("java/lang/String");
298298
jmethodID ctor = env->GetMethodID(cls, "<init>", "([BLjava/lang/String;)V");
299-
jstring object = (jstring) env->NewObject(cls, ctor, array, strEncode);
299+
jstring object = static_cast<jstring>(env->NewObject(cls, ctor, array, strEncode));
300300

301301
const char *cparam = env->GetStringUTFChars(object, NULL);
302302

@@ -332,8 +332,8 @@ std::string JNIHelper::GetStringResource(const std::string& resourceName)
332332
JNIEnv *env = AttachCurrentThread();
333333
jstring name = env->NewStringUTF(resourceName.c_str());
334334

335-
jstring ret = (jstring)
336-
CallObjectMethod("getStringResource", "(Ljava/lang/String;)Ljava/lang/String;", name);
335+
jstring ret = static_cast<jstring>(
336+
CallObjectMethod("getStringResource", "(Ljava/lang/String;)Ljava/lang/String;", name));
337337

338338
const char *resource = env->GetStringUTFChars(ret, NULL);
339339
std::string s = std::string(resource);
@@ -389,7 +389,7 @@ jclass JNIHelper::RetrieveClass(JNIEnv *jni, const char *class_name) {
389389

390390
jstring str_class_name = jni->NewStringUTF(class_name);
391391
jclass class_retrieved =
392-
(jclass) jni->CallObjectMethod(cls, find_class, str_class_name);
392+
static_cast<jclass>(jni->CallObjectMethod(cls, find_class, str_class_name));
393393
jni->DeleteLocalRef(str_class_name);
394394
jni->DeleteLocalRef(activity_class);
395395
jni->DeleteLocalRef(class_loader);
@@ -411,7 +411,7 @@ jstring JNIHelper::GetExternalFilesDirJString(JNIEnv *env) {
411411
jclass cls_File = env->FindClass("java/io/File");
412412
jmethodID mid_getPath =
413413
env->GetMethodID(cls_File, "getPath", "()Ljava/lang/String;");
414-
jstring obj_Path = (jstring) env->CallObjectMethod(obj_File, mid_getPath);
414+
jstring obj_Path = static_cast<jstring>(env->CallObjectMethod(obj_File, mid_getPath));
415415

416416
return obj_Path;
417417
}
@@ -622,15 +622,17 @@ void JNIHelper::RunOnUiThread(std::function<void()> callback) {
622622

623623
// Allocate temporary function object to be passed around
624624
std::function<void()> *pCallback = new std::function<void()>(callback);
625-
env->CallVoidMethod(jni_helper_java_ref_, mid, (int64_t) pCallback);
625+
env->CallVoidMethod(jni_helper_java_ref_, mid, reinterpret_cast<int64_t>(pCallback));
626626
}
627627

628628
// This JNI function is invoked from UIThread asynchronously
629629
extern "C" {
630630
JNIEXPORT void
631631
Java_com_sample_helper_NDKHelper_RunOnUiThreadHandler(JNIEnv *env, jobject thiz,
632632
int64_t pointer) {
633-
std::function<void()> *pCallback = (std::function<void()> *)pointer;
633+
#pragma unused (env)
634+
#pragma unused (thiz)
635+
std::function<void()> *pCallback = reinterpret_cast< std::function<void()> *>(pointer);
634636
(*pCallback)();
635637

636638
// Deleting temporary object

samples-android/Common/NDKHelper/src/main/cpp/JNIHelper.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,11 @@ class JNIHelper {
218218
*/
219219
JNIEnv *AttachCurrentThread() {
220220
JNIEnv *env;
221-
if (activity_->vm->GetEnv((void **)&env, JNI_VERSION_1_4) == JNI_OK)
221+
if (activity_->vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4) == JNI_OK)
222222
return env;
223223
activity_->vm->AttachCurrentThread(&env, NULL);
224-
pthread_key_create((int32_t *)activity_, DetachCurrentThreadDtor);
224+
pthread_key_create(
225+
reinterpret_cast<pthread_key_t *>(activity_), DetachCurrentThreadDtor);
225226
return env;
226227
}
227228

@@ -285,7 +286,7 @@ class JNIHelper {
285286
*/
286287
static void DetachCurrentThreadDtor(void *p) {
287288
LOGI("detached current thread");
288-
ANativeActivity *activity = (ANativeActivity *)p;
289+
ANativeActivity *activity = reinterpret_cast<ANativeActivity *>(p);
289290
activity->vm->DetachCurrentThread();
290291
}
291292

samples-android/Common/NDKHelper/src/main/cpp/perfMonitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace ndk_helper {
2020

2121
PerfMonitor::PerfMonitor()
22-
: current_FPS_(0), tv_last_sec_(0), last_tick_(0.f), tickindex_(0),
22+
: current_FPS_(0), tv_last_sec_(0), last_tick_(0), tickindex_(0),
2323
ticksum_(0) {
2424
for (int32_t i = 0; i < NUM_SAMPLES; ++i)
2525
ticklist_[i] = 0;

samples-android/Common/gpg-sdk/build.gradle

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
/* Copyright (C) 2018 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
115
/*
216
sub module to download the Google Play games
317
*/
418
project.ext {
519
if (!project.hasProperty('gpg_sdk_link')) {
6-
gpg_sdk_link = 'https://developers.google.com/games/services/downloads/gpg-cpp-sdk.v3.0.zip'
20+
gpg_sdk_link = System.getenv("GPG_SDK_LINK")
21+
if (gpg_sdk_link == null || gpg_sdk_link.isEmpty()) {
22+
gpg_sdk_link = 'https://developers.google.com/games/services/downloads/gpg-cpp-sdk.v3.0.zip'
23+
}
724
}
825
}
926
task download_and_stage_gpg_sdk(dependsOn:'unzip_gpg_sdk') {
@@ -24,19 +41,23 @@ task fetch_gpg_cpp_sdk () {
2441
fetch_gpg_cpp_sdk.description = "Download the gpg sdk from the specified location"
2542

2643
task unzip_gpg_sdk() {
44+
def dest = file( "${project.projectDir}/gpg-cpp-sdk")
2745
doFirst {
28-
if (!file('gpg-cpp-sdk').exists()) {
46+
if (!dest.exists()) {
2947
copy {
3048
from(zipTree('gpg_cpp_sdk.zip')) {
3149
exclude 'gpg-cpp-sdk/ios/**/*'
50+
include 'gpg-cpp-sdk/android/**/*'
3251
}
3352

3453
into { project.projectDir }
3554
}
55+
} else {
56+
println "Skipping sdk unzipping, ${dest.absolutePath} exists"
3657
}
3758
}
38-
outputs.dir('gpg-cpp-sdk/android')
39-
outputs.upToDateWhen {file('gpg-cpp-sdk/android').exists()}
59+
outputs.upToDateWhen {file("${dest.absolutePath}/android").exists() &&
60+
file('gpg-cpp-sdk/android/lib/c++/arm64-v8a/libgpg.a').exists()}
4061
description = "Unzips the GPG SDK into the correct dir for NDK"
4162
dependsOn fetch_gpg_cpp_sdk
4263
}
@@ -51,4 +72,4 @@ task clean() {
5172
}
5273
}
5374

54-
defaultTasks = ['download_and_stage_gpg_sdk']
75+
defaultTasks = ['unzip_gpg_sdk']

samples-android/Minimalist/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ android {
3131

3232
externalNativeBuild {
3333
cmake {
34-
cppFlags "-std=c++11 -Wall -frtti"
34+
cppFlags "-std=c++11 -frtti -Wall -Werror"
3535
arguments "-DGPG_SDK_PATH=${project(':Common:gpg-sdk').projectDir}/gpg-cpp-sdk/android",
36-
"-DANDROID_STL=c++_static"
36+
"-DANDROID_STL=c++_static",
37+
"-DANDROID_TOOLCHAIN=clang"
3738
}
3839
}
3940
}

0 commit comments

Comments
 (0)