xref: /aosp_15_r20/external/pigweed/bootstrap.bat (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker:<<"::WINDOWS_ONLY"
2*61c4878aSAndroid Build Coastguard Worker@echo off
3*61c4878aSAndroid Build Coastguard Worker:: Copyright 2020 The Pigweed Authors
4*61c4878aSAndroid Build Coastguard Worker::
5*61c4878aSAndroid Build Coastguard Worker:: Licensed under the Apache License, Version 2.0 (the "License"); you may not
6*61c4878aSAndroid Build Coastguard Worker:: use this file except in compliance with the License. You may obtain a copy of
7*61c4878aSAndroid Build Coastguard Worker:: the License at
8*61c4878aSAndroid Build Coastguard Worker::
9*61c4878aSAndroid Build Coastguard Worker::     https://www.apache.org/licenses/LICENSE-2.0
10*61c4878aSAndroid Build Coastguard Worker::
11*61c4878aSAndroid Build Coastguard Worker:: Unless required by applicable law or agreed to in writing, software
12*61c4878aSAndroid Build Coastguard Worker:: distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13*61c4878aSAndroid Build Coastguard Worker:: WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14*61c4878aSAndroid Build Coastguard Worker:: License for the specific language governing permissions and limitations under
15*61c4878aSAndroid Build Coastguard Worker:: the License.
16*61c4878aSAndroid Build Coastguard Worker::WINDOWS_ONLY
17*61c4878aSAndroid Build Coastguard Worker:; echo "ERROR: Attempting to run Windows .bat from a Unix/POSIX shell!"
18*61c4878aSAndroid Build Coastguard Worker:; echo "Instead, run the following command."
19*61c4878aSAndroid Build Coastguard Worker:; echo ""
20*61c4878aSAndroid Build Coastguard Worker:; echo "    source ./bootstrap.sh"
21*61c4878aSAndroid Build Coastguard Worker:; echo ""
22*61c4878aSAndroid Build Coastguard Worker:<<"::WINDOWS_ONLY"
23*61c4878aSAndroid Build Coastguard Worker
24*61c4878aSAndroid Build Coastguard Worker:: Pigweed Windows environment setup.
25*61c4878aSAndroid Build Coastguard Worker
26*61c4878aSAndroid Build Coastguard Worker:: WARNING: Multi-line "if" statements can be dangerous!
27*61c4878aSAndroid Build Coastguard Worker::
28*61c4878aSAndroid Build Coastguard Worker:: Example:
29*61c4878aSAndroid Build Coastguard Worker::  call do_foo
30*61c4878aSAndroid Build Coastguard Worker::  if [expression] (
31*61c4878aSAndroid Build Coastguard Worker::    call cmd_a
32*61c4878aSAndroid Build Coastguard Worker::    set my_var = %ERRORLEVEL%
33*61c4878aSAndroid Build Coastguard Worker::    call final_script --flag %my_var%
34*61c4878aSAndroid Build Coastguard Worker::  )
35*61c4878aSAndroid Build Coastguard Worker:: Batch evaluates these expressions in a way that will produce unexpected
36*61c4878aSAndroid Build Coastguard Worker:: behavior. It appears that when each line is executed, it does not affect
37*61c4878aSAndroid Build Coastguard Worker:: local context until the entire expression is complete. In this example,
38*61c4878aSAndroid Build Coastguard Worker:: ERRORLEVEL does not reflect `call cmd_a`, but whatever residual state was
39*61c4878aSAndroid Build Coastguard Worker:: present from `do_foo`. Similarly, in the call to `final_script`, `my_var`
40*61c4878aSAndroid Build Coastguard Worker:: will NOT be valid as the variable `set` doesn't apply until the entire `if`
41*61c4878aSAndroid Build Coastguard Worker:: expression completes.
42*61c4878aSAndroid Build Coastguard Worker:: This script only uses multi-line if statements to `goto` after an operation.
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard Worker:: If PW_CHECKOUT_ROOT is set, use it. Users should not set this variable.
45*61c4878aSAndroid Build Coastguard Worker:: It's used because when one batch script invokes another the Powershell magic
46*61c4878aSAndroid Build Coastguard Worker:: below doesn't work. To reinforce that users should not be using
47*61c4878aSAndroid Build Coastguard Worker:: PW_CHECKOUT_ROOT, it is cleared here after it is used, and other pw tools
48*61c4878aSAndroid Build Coastguard Worker:: will complain if they see that variable set.
49*61c4878aSAndroid Build Coastguard Worker:: TODO(mohrr) find out a way to do this without PW_CHECKOUT_ROOT.
50*61c4878aSAndroid Build Coastguard Worker
51*61c4878aSAndroid Build Coastguard Worker:: ~dp0 is the batchism for the directory in which a .bat file resides.
52*61c4878aSAndroid Build Coastguard Workerif "%PW_CHECKOUT_ROOT%"=="" ^
53*61c4878aSAndroid Build Coastguard Workerset "PW_ROOT=%~dp0." &^
54*61c4878aSAndroid Build Coastguard Workergoto select_python
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard Worker:: Since PW_CHECKOUT_ROOT is set, use it.
57*61c4878aSAndroid Build Coastguard Workerset "PW_ROOT=%PW_CHECKOUT_ROOT%"
58*61c4878aSAndroid Build Coastguard Workerset "PW_CHECKOUT_ROOT="
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker:select_python
61*61c4878aSAndroid Build Coastguard Worker:: Allow forcing a specific Python version through the environment variable
62*61c4878aSAndroid Build Coastguard Worker:: PW_BOOTSTRAP_PYTHON. Otherwise, use the system Python if one exists.
63*61c4878aSAndroid Build Coastguard Workerif not "%PW_BOOTSTRAP_PYTHON%" == "" (
64*61c4878aSAndroid Build Coastguard Worker  set "python=%PW_BOOTSTRAP_PYTHON%"
65*61c4878aSAndroid Build Coastguard Worker  goto find_environment_root
66*61c4878aSAndroid Build Coastguard Worker)
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard Worker:: Detect python installation.
69*61c4878aSAndroid Build Coastguard Workerwhere python >NUL 2>&1
70*61c4878aSAndroid Build Coastguard Workerif %ERRORLEVEL% EQU 0 (
71*61c4878aSAndroid Build Coastguard Worker  set "python=python"
72*61c4878aSAndroid Build Coastguard Worker  goto find_environment_root
73*61c4878aSAndroid Build Coastguard Worker)
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard Workerecho.
76*61c4878aSAndroid Build Coastguard Workerecho Error: no system Python present
77*61c4878aSAndroid Build Coastguard Workerecho.
78*61c4878aSAndroid Build Coastguard Workerecho   Pigweed's bootstrap process requires a local system Python.
79*61c4878aSAndroid Build Coastguard Workerecho   Please install Python on your system, add it to your PATH
80*61c4878aSAndroid Build Coastguard Workerecho   and re-try running bootstrap.
81*61c4878aSAndroid Build Coastguard Workergoto finish
82*61c4878aSAndroid Build Coastguard Worker
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard Worker:find_environment_root
85*61c4878aSAndroid Build Coastguard Worker:: PW_ENVIRONMENT_ROOT allows developers to specify where the environment should
86*61c4878aSAndroid Build Coastguard Worker:: be installed. _PW_ACTUAL_ENVIRONMENT_ROOT is where Pigweed keeps that
87*61c4878aSAndroid Build Coastguard Worker:: information. This separation allows Pigweed to assume PW_ENVIRONMENT_ROOT
88*61c4878aSAndroid Build Coastguard Worker:: came from the developer and not from a previous bootstrap possibly from
89*61c4878aSAndroid Build Coastguard Worker:: another workspace.
90*61c4878aSAndroid Build Coastguard Worker
91*61c4878aSAndroid Build Coastguard Workerif "%PW_PROJECT_ROOT%"=="" set "PW_PROJECT_ROOT=%PW_ROOT%"
92*61c4878aSAndroid Build Coastguard Worker
93*61c4878aSAndroid Build Coastguard Workerif "%PW_ENVIRONMENT_ROOT%"=="" (
94*61c4878aSAndroid Build Coastguard Worker  set "_PW_ACTUAL_ENVIRONMENT_ROOT=%PW_PROJECT_ROOT%\environment"
95*61c4878aSAndroid Build Coastguard Worker) else (
96*61c4878aSAndroid Build Coastguard Worker  set "_PW_ACTUAL_ENVIRONMENT_ROOT=%PW_ENVIRONMENT_ROOT%"
97*61c4878aSAndroid Build Coastguard Worker)
98*61c4878aSAndroid Build Coastguard Worker
99*61c4878aSAndroid Build Coastguard Workerset "shell_file=%_PW_ACTUAL_ENVIRONMENT_ROOT%\activate.bat"
100*61c4878aSAndroid Build Coastguard Worker
101*61c4878aSAndroid Build Coastguard Workerset "_pw_start_script=%PW_ROOT%\pw_env_setup\py\pw_env_setup\windows_env_start.py"
102*61c4878aSAndroid Build Coastguard Worker
103*61c4878aSAndroid Build Coastguard Worker:: If PW_SKIP_BOOTSTRAP is set, only run the activation stage instead of the
104*61c4878aSAndroid Build Coastguard Worker:: complete env_setup.
105*61c4878aSAndroid Build Coastguard Workerif not "%PW_SKIP_BOOTSTRAP%" == "" goto skip_bootstrap
106*61c4878aSAndroid Build Coastguard Worker
107*61c4878aSAndroid Build Coastguard Worker:: Without the trailing slash in %PW_ROOT%/, batch combines that token with
108*61c4878aSAndroid Build Coastguard Worker:: the --shell-file argument.
109*61c4878aSAndroid Build Coastguard Workercall "%python%" "%PW_ROOT%\pw_env_setup\py\pw_env_setup\env_setup.py" ^
110*61c4878aSAndroid Build Coastguard Worker    --pw-root "%PW_ROOT%" ^
111*61c4878aSAndroid Build Coastguard Worker    --shell-file "%shell_file%" ^
112*61c4878aSAndroid Build Coastguard Worker    --install-dir "%_PW_ACTUAL_ENVIRONMENT_ROOT%" ^
113*61c4878aSAndroid Build Coastguard Worker    --project-root "%PW_PROJECT_ROOT%"
114*61c4878aSAndroid Build Coastguard Workergoto activate_shell
115*61c4878aSAndroid Build Coastguard Worker
116*61c4878aSAndroid Build Coastguard Worker:skip_bootstrap
117*61c4878aSAndroid Build Coastguard Workerif exist "%shell_file%" (
118*61c4878aSAndroid Build Coastguard Worker  call "%python%" "%_pw_start_script%"
119*61c4878aSAndroid Build Coastguard Worker) else (
120*61c4878aSAndroid Build Coastguard Worker  call "%python%" "%_pw_start_script%" --no-shell-file
121*61c4878aSAndroid Build Coastguard Worker  goto finish
122*61c4878aSAndroid Build Coastguard Worker)
123*61c4878aSAndroid Build Coastguard Worker
124*61c4878aSAndroid Build Coastguard Worker:activate_shell
125*61c4878aSAndroid Build Coastguard Workerif exist "%shell_file%" (
126*61c4878aSAndroid Build Coastguard Worker  call "%shell_file%"
127*61c4878aSAndroid Build Coastguard Worker) else (
128*61c4878aSAndroid Build Coastguard Worker  echo.
129*61c4878aSAndroid Build Coastguard Worker  echo  Bootstrap failed! See output above for the culprit.
130*61c4878aSAndroid Build Coastguard Worker  echo.
131*61c4878aSAndroid Build Coastguard Worker)
132*61c4878aSAndroid Build Coastguard Worker
133*61c4878aSAndroid Build Coastguard Worker:finish
134*61c4878aSAndroid Build Coastguard Worker::WINDOWS_ONLY
135