1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdlib.h>
18 #include <unistd.h>
19
20 #include "android-base/logging.h"
21
22 #include "common/libs/utils/environment.h"
23 #include "common/libs/utils/files.h"
24
25 namespace cuttlefish {
26
27 namespace {
28
29 constexpr char kCvdBinaryPath[] = "/usr/bin/cvd";
30
FallbackToPythonAcloud(char ** argv)31 int FallbackToPythonAcloud(char** argv) {
32 auto android_top = StringFromEnv("ANDROID_BUILD_TOP", "");
33 if (android_top == "") {
34 LOG(FATAL) << "Could not find android environment. Please run "
35 << "\"source build/envsetup.sh\".";
36 abort();
37 }
38 // TODO(b/206893146): Detect what the platform actually is.
39 auto py_acloud_path =
40 android_top + "/prebuilts/asuite/acloud/linux-x86/acloud";
41
42 return execv(py_acloud_path.c_str(), argv);
43 }
44
TranslatorMain(char ** argv)45 int TranslatorMain(char** argv) {
46 if (!FileExists(kCvdBinaryPath, true /*follow symlinks*/)) {
47 LOG(WARNING)
48 << "The host packages may not be installed or are old, "
49 "consider running `acloud setup --host` to get the latest features.";
50 return FallbackToPythonAcloud(argv);
51 }
52
53 // This executes /usr/bin/cvd with argv[0] = "acloud", which triggers the
54 // translator flow and can still fallback to the python prebuilt if needed
55 // using the environment.
56 return execv(kCvdBinaryPath, argv);
57 }
58
59 } // namespace
60
61 } // namespace cuttlefish
62
main(int,char ** argv)63 int main(int, char** argv) {
64 android::base::InitLogging(argv, android::base::StderrLogger);
65 return cuttlefish::TranslatorMain(argv);
66 }
67