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