16508be1224ddec08910c464d2a905c4c2e1f7d80Raphael/*
26508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * Copyright (C) 2009 The Android Open Source Project
36508be1224ddec08910c464d2a905c4c2e1f7d80Raphael *
46508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * Licensed under the Apache License, Version 2.0 (the "License");
56508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * you may not use this file except in compliance with the License.
66508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * You may obtain a copy of the License at
76508be1224ddec08910c464d2a905c4c2e1f7d80Raphael *
86508be1224ddec08910c464d2a905c4c2e1f7d80Raphael *      http://www.apache.org/licenses/LICENSE-2.0
96508be1224ddec08910c464d2a905c4c2e1f7d80Raphael *
106508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * Unless required by applicable law or agreed to in writing, software
116508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * distributed under the License is distributed on an "AS IS" BASIS,
126508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * See the License for the specific language governing permissions and
146508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * limitations under the License.
156508be1224ddec08910c464d2a905c4c2e1f7d80Raphael */
166508be1224ddec08910c464d2a905c4c2e1f7d80Raphael
176508be1224ddec08910c464d2a905c4c2e1f7d80Raphael/*
180913421ffd4ab078bc2f9472c73f1e5bbf9525c4Raphael Moll * The "SDK Manager" is for Windows only.
196508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * This simple .exe will sit at the root of the Windows SDK
206508be1224ddec08910c464d2a905c4c2e1f7d80Raphael * and currently simply executes tools\android.bat.
216508be1224ddec08910c464d2a905c4c2e1f7d80Raphael *
22fb098496a1460ed7b4bab997bc2acd96934236e7Raphael * TODO: replace by a jar-exe wrapper.
236508be1224ddec08910c464d2a905c4c2e1f7d80Raphael */
246508be1224ddec08910c464d2a905c4c2e1f7d80Raphael
256508be1224ddec08910c464d2a905c4c2e1f7d80Raphael#ifdef _WIN32
266508be1224ddec08910c464d2a905c4c2e1f7d80Raphael
276508be1224ddec08910c464d2a905c4c2e1f7d80Raphael#include <stdio.h>
28886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll#include <stdarg.h>
29886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll#include <string.h>
306508be1224ddec08910c464d2a905c4c2e1f7d80Raphael#include <windows.h>
316508be1224ddec08910c464d2a905c4c2e1f7d80Raphael
32ea66c92f80d9745721cfa28e22f2726b76579158Raphael
33886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Mollint _enable_dprintf = 0;
34886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll
35886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Mollvoid dprintf(char *msg, ...) {
36886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    va_list ap;
37886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    va_start(ap, msg);
38886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll
39886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    if (_enable_dprintf) {
40886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        vfprintf(stderr, msg, ap);
41886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    }
42886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll
43886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    va_end(ap);
44886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll}
45886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll
46ea66c92f80d9745721cfa28e22f2726b76579158Raphaelvoid display_error(LPSTR description) {
47ea66c92f80d9745721cfa28e22f2726b76579158Raphael    DWORD err = GetLastError();
48ea66c92f80d9745721cfa28e22f2726b76579158Raphael    LPSTR s, s2;
49ea66c92f80d9745721cfa28e22f2726b76579158Raphael
50ea66c92f80d9745721cfa28e22f2726b76579158Raphael    fprintf(stderr, "%s, error %ld\n", description, err);
51ea66c92f80d9745721cfa28e22f2726b76579158Raphael
52ea66c92f80d9745721cfa28e22f2726b76579158Raphael    if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | /* dwFlags */
53ea66c92f80d9745721cfa28e22f2726b76579158Raphael                      FORMAT_MESSAGE_FROM_SYSTEM,
54ea66c92f80d9745721cfa28e22f2726b76579158Raphael                      NULL,                             /* lpSource */
55ea66c92f80d9745721cfa28e22f2726b76579158Raphael                      err,                              /* dwMessageId */
56ea66c92f80d9745721cfa28e22f2726b76579158Raphael                      0,                                /* dwLanguageId */
57ea66c92f80d9745721cfa28e22f2726b76579158Raphael                      (LPSTR)&s,                        /* lpBuffer */
58ea66c92f80d9745721cfa28e22f2726b76579158Raphael                      0,                                /* nSize */
59ea66c92f80d9745721cfa28e22f2726b76579158Raphael                      NULL) != 0) {                     /* va_list args */
60ea66c92f80d9745721cfa28e22f2726b76579158Raphael        fprintf(stderr, "%s", s);
61ea66c92f80d9745721cfa28e22f2726b76579158Raphael
62ea66c92f80d9745721cfa28e22f2726b76579158Raphael        s2 = (LPSTR) malloc(strlen(description) + strlen(s) + 5);
63ea66c92f80d9745721cfa28e22f2726b76579158Raphael        sprintf(s2, "%s\r\n%s", description, s);
640913421ffd4ab078bc2f9472c73f1e5bbf9525c4Raphael Moll        MessageBox(NULL, s2, "Android SDK Manager - Error", MB_OK);
65ea66c92f80d9745721cfa28e22f2726b76579158Raphael        free(s2);
66ea66c92f80d9745721cfa28e22f2726b76579158Raphael        LocalFree(s);
67ea66c92f80d9745721cfa28e22f2726b76579158Raphael    }
68ea66c92f80d9745721cfa28e22f2726b76579158Raphael}
69ea66c92f80d9745721cfa28e22f2726b76579158Raphael
70ea66c92f80d9745721cfa28e22f2726b76579158Raphael
716508be1224ddec08910c464d2a905c4c2e1f7d80Raphaelint sdk_launcher() {
72ea66c92f80d9745721cfa28e22f2726b76579158Raphael    int                   result = 0;
736508be1224ddec08910c464d2a905c4c2e1f7d80Raphael    STARTUPINFO           startup;
746508be1224ddec08910c464d2a905c4c2e1f7d80Raphael    PROCESS_INFORMATION   pinfo;
75886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    CHAR                  program_dir[MAX_PATH];
76886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    int                   ret, pos;
77ea66c92f80d9745721cfa28e22f2726b76579158Raphael
78ea66c92f80d9745721cfa28e22f2726b76579158Raphael    ZeroMemory(&pinfo, sizeof(pinfo));
79ea66c92f80d9745721cfa28e22f2726b76579158Raphael
806508be1224ddec08910c464d2a905c4c2e1f7d80Raphael    ZeroMemory(&startup, sizeof(startup));
816508be1224ddec08910c464d2a905c4c2e1f7d80Raphael    startup.cb = sizeof(startup);
8258fd50795e20449fcf880022a9debbbcb2357376Raphael    startup.dwFlags     = STARTF_USESHOWWINDOW;
8358fd50795e20449fcf880022a9debbbcb2357376Raphael    startup.wShowWindow = SW_HIDE|SW_MINIMIZE;
846508be1224ddec08910c464d2a905c4c2e1f7d80Raphael
85886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    /* get path of current program, to switch dirs here when executing the command. */
86886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    ret = GetModuleFileName(NULL, program_dir, sizeof(program_dir));
87886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    if (ret == 0) {
88886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        display_error("Failed to get program's filename:");
89ea66c92f80d9745721cfa28e22f2726b76579158Raphael        result = 1;
90ea66c92f80d9745721cfa28e22f2726b76579158Raphael    } else {
91886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        /* Remove the last segment to keep only the directory. */
92886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        pos = ret - 1;
93886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        while (pos > 0 && program_dir[pos] != '\\') {
94886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll            --pos;
95886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        }
96886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        program_dir[pos] = 0;
97886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    }
98886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll
99886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    if (!result) {
100886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        dprintf("Program dir: %s\n", program_dir);
101886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll
102886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        ret = CreateProcess(
103886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll                NULL,                                       /* program path */
104fb098496a1460ed7b4bab997bc2acd96934236e7Raphael                "tools\\android.bat sdk",                   /* command-line */
105886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll                NULL,                  /* process handle is not inheritable */
106886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll                NULL,                   /* thread handle is not inheritable */
107886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll                TRUE,                          /* yes, inherit some handles */
108886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll                CREATE_NO_WINDOW,                /* we don't want a console */
109886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll                NULL,                     /* use parent's environment block */
110886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll                program_dir,             /* use parent's starting directory */
111886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll                &startup,                 /* startup info, i.e. std handles */
112886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll                &pinfo);
113fb098496a1460ed7b4bab997bc2acd96934236e7Raphael
114886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        dprintf("CreateProcess returned %d\n", ret);
115886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll
116886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        if (!ret) {
117886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll            display_error("Failed to execute tools\\android.bat:");
118886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll            result = 1;
119886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll        }
120ea66c92f80d9745721cfa28e22f2726b76579158Raphael    }
121fb098496a1460ed7b4bab997bc2acd96934236e7Raphael
122886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    dprintf("Cleanup.\n");
123ea66c92f80d9745721cfa28e22f2726b76579158Raphael
124ea66c92f80d9745721cfa28e22f2726b76579158Raphael    return result;
1256508be1224ddec08910c464d2a905c4c2e1f7d80Raphael}
1266508be1224ddec08910c464d2a905c4c2e1f7d80Raphael
1276508be1224ddec08910c464d2a905c4c2e1f7d80Raphaelint main(int argc, char **argv) {
128886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    _enable_dprintf = argc > 1 && strcmp(argv[1], "-v") == 0;
129886a4f368cf3e793403b1b637e8f54f7a3ffb8e4Raphael Moll    dprintf("Verbose debug mode.\n");
130fb098496a1460ed7b4bab997bc2acd96934236e7Raphael
1316508be1224ddec08910c464d2a905c4c2e1f7d80Raphael    return sdk_launcher();
1326508be1224ddec08910c464d2a905c4c2e1f7d80Raphael}
1336508be1224ddec08910c464d2a905c4c2e1f7d80Raphael
1346508be1224ddec08910c464d2a905c4c2e1f7d80Raphael#endif /* _WIN32 */
135