blob: 6b2c45c99cdb367136ab2b811a24c6ca386d18b8 [file] [log] [blame]
Raphael6508be12009-10-11 22:32:49 -07001/*
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/*
Raphael Moll09134212010-08-21 18:03:49 -070018 * The "SDK Manager" is for Windows only.
Raphael6508be12009-10-11 22:32:49 -070019 * This simple .exe will sit at the root of the Windows SDK
20 * and currently simply executes tools\android.bat.
Raphael6508be12009-10-11 22:32:49 -070021 *
Raphaelfb098492011-09-16 09:18:08 -070022 * TODO: replace by a jar-exe wrapper.
Raphael6508be12009-10-11 22:32:49 -070023 */
24
25#ifdef _WIN32
26
27#include <stdio.h>
Raphael Moll886a4f32010-04-28 14:23:50 -070028#include <stdarg.h>
29#include <string.h>
Raphael6508be12009-10-11 22:32:49 -070030#include <windows.h>
31
Raphaelea66c922010-01-27 12:48:34 -080032
Raphael Moll886a4f32010-04-28 14:23:50 -070033int _enable_dprintf = 0;
34
35void 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
Raphaelea66c922010-01-27 12:48:34 -080046void 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);
Raphael Moll09134212010-08-21 18:03:49 -070064 MessageBox(NULL, s2, "Android SDK Manager - Error", MB_OK);
Raphaelea66c922010-01-27 12:48:34 -080065 free(s2);
66 LocalFree(s);
67 }
68}
69
70
Raphael6508be12009-10-11 22:32:49 -070071int sdk_launcher() {
Raphaelea66c922010-01-27 12:48:34 -080072 int result = 0;
Raphael6508be12009-10-11 22:32:49 -070073 STARTUPINFO startup;
74 PROCESS_INFORMATION pinfo;
Raphael Moll886a4f32010-04-28 14:23:50 -070075 CHAR program_dir[MAX_PATH];
76 int ret, pos;
Raphaelea66c922010-01-27 12:48:34 -080077
78 ZeroMemory(&pinfo, sizeof(pinfo));
79
Raphael6508be12009-10-11 22:32:49 -070080 ZeroMemory(&startup, sizeof(startup));
81 startup.cb = sizeof(startup);
Raphael58fd5072011-11-11 10:43:03 -080082 startup.dwFlags = STARTF_USESHOWWINDOW;
83 startup.wShowWindow = SW_HIDE|SW_MINIMIZE;
Raphael6508be12009-10-11 22:32:49 -070084
Raphael Moll886a4f32010-04-28 14:23:50 -070085 /* 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:");
Raphaelea66c922010-01-27 12:48:34 -080089 result = 1;
90 } else {
Raphael Moll886a4f32010-04-28 14:23:50 -070091 /* 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;
Raphael6508be12009-10-11 22:32:49 -070097 }
98
Raphael Moll886a4f32010-04-28 14:23:50 -070099 if (!result) {
100 dprintf("Program dir: %s\n", program_dir);
101
Raphael Molld5d08c02012-11-19 16:41:08 -0800102 // SDK Manager.exe is installed by the Windows Installer just below
103 // the tools directory and needs to access tools\android.bat
Raphael Moll886a4f32010-04-28 14:23:50 -0700104 ret = CreateProcess(
105 NULL, /* program path */
Raphaelfb098492011-09-16 09:18:08 -0700106 "tools\\android.bat sdk", /* command-line */
Raphael Moll886a4f32010-04-28 14:23:50 -0700107 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);
Raphaelfb098492011-09-16 09:18:08 -0700115
Raphael Molld5d08c02012-11-19 16:41:08 -0800116 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
Raphael Moll886a4f32010-04-28 14:23:50 -0700134 dprintf("CreateProcess returned %d\n", ret);
135
136 if (!ret) {
137 display_error("Failed to execute tools\\android.bat:");
138 result = 1;
Raphael Moll886a4f32010-04-28 14:23:50 -0700139 }
140 }
Raphaelfb098492011-09-16 09:18:08 -0700141
Raphael Moll886a4f32010-04-28 14:23:50 -0700142 dprintf("Cleanup.\n");
143
Raphaelea66c922010-01-27 12:48:34 -0800144 return result;
Raphael6508be12009-10-11 22:32:49 -0700145}
146
147int main(int argc, char **argv) {
Raphael Moll886a4f32010-04-28 14:23:50 -0700148 _enable_dprintf = argc > 1 && strcmp(argv[1], "-v") == 0;
149 dprintf("Verbose debug mode.\n");
Raphaelfb098492011-09-16 09:18:08 -0700150
Raphael6508be12009-10-11 22:32:49 -0700151 return sdk_launcher();
152}
153
154#endif /* _WIN32 */