1 /*
2  * Copyright (C) 2015, The Android Open Source Project *
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 
16 #ifndef AIDL_OPTIONS_H_
17 #define AIDL_OPTIONS_H_
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include <android-base/macros.h>
24 #include <gtest/gtest_prod.h>
25 
26 namespace android {
27 namespace aidl {
28 
29 // This object represents the parsed options to the Java generating aidl.
30 class JavaOptions final {
31  public:
32   enum {
33       COMPILE_AIDL_TO_JAVA,
34       PREPROCESS_AIDL,
35   };
36 
37   ~JavaOptions() = default;
38 
39   // Parses the command line and returns a non-null pointer to an JavaOptions
40   // object on success.
41   // Prints the usage statement on failure.
42   static std::unique_ptr<JavaOptions> Parse(int argc, const char* const* argv);
43 
44   std::string DependencyFilePath() const;
45 
46   int task{COMPILE_AIDL_TO_JAVA};
47   bool fail_on_parcelable_{false};
48   std::vector<std::string> import_paths_;
49   std::vector<std::string> preprocessed_files_;
50   std::string input_file_name_;
51   std::string output_file_name_;
52   std::string output_base_folder_;
53   std::string dep_file_name_;
54   bool auto_dep_file_{false};
55   std::vector<std::string> files_to_preprocess_;
56 
57  private:
58   JavaOptions() = default;
59 
60   FRIEND_TEST(EndToEndTest, IExampleInterface);
61   FRIEND_TEST(AidlTest, FailOnParcelable);
62   FRIEND_TEST(AidlTest, WritePreprocessedFile);
63   FRIEND_TEST(AidlTest, WritesCorrectDependencyFile);
64   FRIEND_TEST(AidlTest, WritesTrivialDependencyFileForParcelable);
65 
66   DISALLOW_COPY_AND_ASSIGN(JavaOptions);
67 };
68 
69 class CppOptions final {
70  public:
71 
72   ~CppOptions() = default;
73 
74   // Parses the command line and returns a non-null pointer to an CppOptions
75   // object on success.
76   // Prints the usage statement on failure.
77   static std::unique_ptr<CppOptions> Parse(int argc, const char* const* argv);
78 
InputFileName()79   std::string InputFileName() const { return input_file_name_; }
OutputHeaderDir()80   std::string OutputHeaderDir() const { return output_header_dir_; }
OutputCppFilePath()81   std::string OutputCppFilePath() const { return output_file_name_; }
82 
ImportPaths()83   std::vector<std::string> ImportPaths() const { return import_paths_; }
DependencyFilePath()84   std::string DependencyFilePath() const { return dep_file_name_; }
85 
86  private:
87   CppOptions() = default;
88 
89   std::string input_file_name_;
90   std::vector<std::string> import_paths_;
91   std::string output_header_dir_;
92   std::string output_file_name_;
93   std::string dep_file_name_;
94 
95   FRIEND_TEST(CppOptionsTests, ParsesCompileCpp);
96   DISALLOW_COPY_AND_ASSIGN(CppOptions);
97 };
98 
99 bool EndsWith(const std::string& str, const std::string& suffix);
100 bool ReplaceSuffix(const std::string& old_suffix,
101                    const std::string& new_suffix,
102                    std::string* str);
103 
104 }  // namespace android
105 }  // namespace aidl
106 
107 #endif // AIDL_OPTIONS_H_
108