1 /*
2 * Copyright (C) 2009 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 /*
18 * The "SDK Manager" is for Windows only.
19 * This simple .exe will sit at the root of the Windows SDK
20 * and currently simply executes tools\android.bat.
21 *
22 * TODO: replace by a jar-exe wrapper.
23 */
24
25 #ifdef _WIN32
26
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <windows.h>
31
32
33 int _enable_dprintf = 0;
34
dprintf(char * msg,...)35 void dprintf(char *msg, ...) {
36 va_list ap;
37 va_start(ap, msg);
38
39 if (_enable_dprintf) {
40 vfprintf(stderr, msg, ap);
41 }
42
43 va_end(ap);
44 }
45
display_error(LPSTR description)46 void display_error(LPSTR description) {
47 DWORD err = GetLastError();
48 LPSTR s, s2;
49
50 fprintf(stderr, "%s, error %ld\n", description, err);
51
52 if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | /* dwFlags */
53 FORMAT_MESSAGE_FROM_SYSTEM,
54 NULL, /* lpSource */
55 err, /* dwMessageId */
56 0, /* dwLanguageId */
57 (LPSTR)&s, /* lpBuffer */
58 0, /* nSize */
59 NULL) != 0) { /* va_list args */
60 fprintf(stderr, "%s", s);
61
62 s2 = (LPSTR) malloc(strlen(description) + strlen(s) + 5);
63 sprintf(s2, "%s\r\n%s", description, s);
64 MessageBox(NULL, s2, "Android SDK Manager - Error", MB_OK);
65 free(s2);
66 LocalFree(s);
67 }
68 }
69
70
sdk_launcher()71 int sdk_launcher() {
72 int result = 0;
73 STARTUPINFO startup;
74 PROCESS_INFORMATION pinfo;
75 CHAR program_dir[MAX_PATH];
76 int ret, pos;
77
78 ZeroMemory(&pinfo, sizeof(pinfo));
79
80 ZeroMemory(&startup, sizeof(startup));
81 startup.cb = sizeof(startup);
82 startup.dwFlags = STARTF_USESHOWWINDOW;
83 startup.wShowWindow = SW_HIDE|SW_MINIMIZE;
84
85 /* get path of current program, to switch dirs here when executing the command. */
86 ret = GetModuleFileName(NULL, program_dir, sizeof(program_dir));
87 if (ret == 0) {
88 display_error("Failed to get program's filename:");
89 result = 1;
90 } else {
91 /* Remove the last segment to keep only the directory. */
92 pos = ret - 1;
93 while (pos > 0 && program_dir[pos] != '\\') {
94 --pos;
95 }
96 program_dir[pos] = 0;
97 }
98
99 if (!result) {
100 dprintf("Program dir: %s\n", program_dir);
101
102 // SDK Manager.exe is installed by the Windows Installer just below
103 // the tools directory and needs to access tools\android.bat
104 ret = CreateProcess(
105 NULL, /* program path */
106 "tools\\android.bat sdk", /* command-line */
107 NULL, /* process handle is not inheritable */
108 NULL, /* thread handle is not inheritable */
109 TRUE, /* yes, inherit some handles */
110 CREATE_NO_WINDOW, /* we don't want a console */
111 NULL, /* use parent's environment block */
112 program_dir, /* use parent's starting directory */
113 &startup, /* startup info, i.e. std handles */
114 &pinfo);
115
116 if (!ret) {
117 dprintf("CreateProcess returned %d\n", ret);
118
119 // In the ADT bundle, SDK Manager.exe is located in the sdk folder
120 // and needs to access sdk\tools\android.bat
121 ret = CreateProcess(
122 NULL, /* program path */
123 "sdk\\tools\\android.bat sdk", /* command-line */
124 NULL, /* process handle is not inheritable */
125 NULL, /* thread handle is not inheritable */
126 TRUE, /* yes, inherit some handles */
127 CREATE_NO_WINDOW, /* we don't want a console */
128 NULL, /* use parent's environment block */
129 program_dir, /* use parent's starting directory */
130 &startup, /* startup info, i.e. std handles */
131 &pinfo);
132 }
133
134 dprintf("CreateProcess returned %d\n", ret);
135
136 if (!ret) {
137 display_error("Failed to execute tools\\android.bat:");
138 result = 1;
139 }
140 }
141
142 dprintf("Cleanup.\n");
143
144 return result;
145 }
146
main(int argc,char ** argv)147 int main(int argc, char **argv) {
148 _enable_dprintf = argc > 1 && strcmp(argv[1], "-v") == 0;
149 dprintf("Verbose debug mode.\n");
150
151 return sdk_launcher();
152 }
153
154 #endif /* _WIN32 */
155