• 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 main(int argc, const char** argv);
22 
Java_com_google_amber_Amber_androidMain(JNIEnv * env,jobject,jobjectArray args,jstring stdoutFile,jstring stderrFile)23 extern "C" JNIEXPORT JNICALL int Java_com_google_amber_Amber_androidMain(
24     JNIEnv* env,
25     jobject,
26     jobjectArray args,
27     jstring stdoutFile,
28     jstring stderrFile) {
29   const char* stdout_file_cstr = env->GetStringUTFChars(stdoutFile, NULL);
30   const char* stderr_file_cstr = env->GetStringUTFChars(stderrFile, NULL);
31 
32   // Redirect std output to a file
33   freopen(stdout_file_cstr, "w", stdout);
34   freopen(stderr_file_cstr, "w", stderr);
35 
36   env->ReleaseStringUTFChars(stdoutFile, stdout_file_cstr);
37   env->ReleaseStringUTFChars(stderrFile, stderr_file_cstr);
38 
39   jsize arg_count = env->GetArrayLength(args);
40 
41   std::vector<std::string> argv_string;
42   argv_string.push_back("amber");
43 
44   for (jsize i = 0; i < arg_count; i++) {
45     jstring js = static_cast<jstring>(env->GetObjectArrayElement(args, i));
46     const char* arg_cstr = env->GetStringUTFChars(js, NULL);
47     argv_string.push_back(arg_cstr);
48     env->ReleaseStringUTFChars(js, arg_cstr);
49   }
50 
51   std::vector<const char*> argv;
52 
53   for (const std::string& arg : argv_string)
54     argv.push_back(arg.c_str());
55 
56   return main(argv.size(), argv.data());
57 }
58