• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Amber Authors.
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 
15 #include <jni.h>
16 
17 #include <sstream>
18 #include <string>
19 #include <vector>
20 
21 extern int android_main(int argc, const char** argv);
22 
23 #pragma clang diagnostic push
24 #pragma clang diagnostic ignored "-Wmissing-prototypes"
25 #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
26 
Java_com_google_amber_Amber_androidHelper(JNIEnv * env,jobject,jobjectArray args,jstring stdoutFile,jstring stderrFile)27 extern "C" JNIEXPORT JNICALL int Java_com_google_amber_Amber_androidHelper(
28     JNIEnv* env,
29     jobject,
30     jobjectArray args,
31     jstring stdoutFile,
32     jstring stderrFile) {
33   const char* stdout_file_cstr = env->GetStringUTFChars(stdoutFile, nullptr);
34   const char* stderr_file_cstr = env->GetStringUTFChars(stderrFile, nullptr);
35 
36   // Redirect std output to a file
37   freopen(stdout_file_cstr, "w", stdout);
38   freopen(stderr_file_cstr, "w", stderr);
39 
40   env->ReleaseStringUTFChars(stdoutFile, stdout_file_cstr);
41   env->ReleaseStringUTFChars(stderrFile, stderr_file_cstr);
42 
43   jsize arg_count = env->GetArrayLength(args);
44 
45   std::vector<std::string> argv_string;
46   argv_string.push_back("amber");
47 
48   for (jsize i = 0; i < arg_count; i++) {
49     jstring js = static_cast<jstring>(env->GetObjectArrayElement(args, i));
50     const char* arg_cstr = env->GetStringUTFChars(js, nullptr);
51     argv_string.push_back(arg_cstr);
52     env->ReleaseStringUTFChars(js, arg_cstr);
53   }
54 
55   std::vector<const char*> argv;
56 
57   for (const std::string& arg : argv_string)
58     argv.push_back(arg.c_str());
59 
60   return android_main(static_cast<int>(argv.size()), argv.data());
61 }
62 
63 #pragma clang diagnostic pop
64