idf.py.exe wrapper program for Windows
This commit is contained in:
parent
1a2bf4d8ff
commit
916c0c5754
4 changed files with 177 additions and 0 deletions
25
tools/windows/idf_exe/CMakeLists.txt
Normal file
25
tools/windows/idf_exe/CMakeLists.txt
Normal file
|
@ -0,0 +1,25 @@
|
|||
cmake_minimum_required(VERSION 3.5)
|
||||
project(idfexe)
|
||||
|
||||
set(VERSION 1.0)
|
||||
set(ARCHIVE_NAME idf-exe-v${VERSION}.zip)
|
||||
|
||||
add_executable(idf idf_main.c)
|
||||
set_target_properties(idf PROPERTIES C_STANDARD 99)
|
||||
target_link_libraries(idf "-lshlwapi")
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL Release)
|
||||
add_custom_command(TARGET idf
|
||||
POST_BUILD
|
||||
COMMAND ${CMAKE_STRIP} idf.exe)
|
||||
endif()
|
||||
|
||||
add_custom_target(dist ALL DEPENDS idf)
|
||||
|
||||
add_custom_command(
|
||||
TARGET dist
|
||||
POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${CMAKE_CURRENT_BINARY_DIR}/idf.exe" "${CMAKE_CURRENT_BINARY_DIR}/idf.py.exe"
|
||||
COMMAND ${CMAKE_COMMAND} ARGS -E tar cfv ${ARCHIVE_NAME} --format=zip
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/idf.py.exe"
|
||||
)
|
34
tools/windows/idf_exe/README.md
Normal file
34
tools/windows/idf_exe/README.md
Normal file
|
@ -0,0 +1,34 @@
|
|||
# IDF wrapper tool (idf.py.exe)
|
||||
|
||||
This tools helps invoke idf.py in Windows CMD shell.
|
||||
|
||||
In Windows CMD shell, python scripts can be executed directly (by typing their name) if `.py` extension is associated with Python. The issue with such association is that it is incompatible with virtual environments. That is, if `.py` extension is associated with a global (or user-specific) copy of `python.exe`, then the virtual environment will not be used when running the script. [Python Launcher](https://www.python.org/dev/peps/pep-0397/) solves this issue, but it is installed by default only with Python 3.6 and newer. In addition to that, the user may choose not to install Python Launcher (for example, to keep `.py` files associated with an editor).
|
||||
|
||||
Hence, `idf.py.exe` is introduced. It is a simple program which forwards the command line arguments to `idf.py`. That is,
|
||||
|
||||
```
|
||||
idf.py args...
|
||||
```
|
||||
has the same effect as:
|
||||
|
||||
```
|
||||
python.exe %IDF_PATH%\tools\idf.py args...
|
||||
```
|
||||
|
||||
`python.exe` location is determined using the default search rules, which include searching the directories in `%PATH%`. Standard I/O streams are forwarded between `idf.py.exe` and `python.exe` processes. The exit code of `idf.py.exe` is the same as the exit code of `python.exe` process.
|
||||
|
||||
For compatibility with `idf_tools.py`, a flag to obtain the version of `idf.py.exe` is provided: `idf.py.exe -v` or `idf.py.exe --version`. Note that this flag only works when `idf.py.exe` is started by the full name (with `.exe` extension). Running `idf.py -v` results in same behavior as `python idf.py -v`, that is `-v` argument is propagated to the Python script.
|
||||
|
||||
## Building
|
||||
|
||||
On Linux/Mac, install mingw-w64 toolchain (`i686-w64-mingw32-gcc`). Then build `idf.py.exe` using CMake:
|
||||
|
||||
```
|
||||
mkdir -p build
|
||||
cd build
|
||||
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain-i686-w64-mingw32.cmake -DCMAKE_BUILD_TYPE=Release ..
|
||||
cmake --build .
|
||||
```
|
||||
|
||||
On Windows, it is also possible to build using Visual Studio, with CMake support installed.
|
||||
|
111
tools/windows/idf_exe/idf_main.c
Normal file
111
tools/windows/idf_exe/idf_main.c
Normal file
|
@ -0,0 +1,111 @@
|
|||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at",
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License
|
||||
|
||||
#include <windows.h>
|
||||
#include <shlwapi.h>
|
||||
#include <strsafe.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define LINESIZE 1024
|
||||
#define VERSION "1.0"
|
||||
|
||||
static void fail(LPCSTR message, ...) __attribute__((noreturn));
|
||||
|
||||
static void fail(LPCSTR message, ...)
|
||||
{
|
||||
DWORD written;
|
||||
char msg[LINESIZE];
|
||||
va_list args = NULL;
|
||||
va_start(args, message);
|
||||
StringCchVPrintfA(msg, sizeof(msg), message, args);
|
||||
WriteFile(GetStdHandle(STD_ERROR_HANDLE), message, lstrlen(msg), &written, NULL);
|
||||
ExitProcess(1);
|
||||
}
|
||||
|
||||
int main(int argc, LPTSTR argv[])
|
||||
{
|
||||
/* Print the version of this wrapper tool, but only if invoked as "idf.exe".
|
||||
* "idf -v" will invoke idf.py as expected.
|
||||
*/
|
||||
|
||||
LPCTSTR cmdname = PathFindFileName(argv[0]);
|
||||
int cmdname_length = strlen(cmdname);
|
||||
|
||||
if (argc == 2 &&
|
||||
cmdname_length > 4 &&
|
||||
StrCmp(cmdname + cmdname_length - 4, TEXT(".exe")) == 0 &&
|
||||
(StrCmp(argv[1], TEXT("--version")) == 0 ||
|
||||
StrCmp(argv[1], TEXT("-v")) == 0)) {
|
||||
LPCSTR msg = VERSION "\n";
|
||||
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msg, lstrlen(msg), NULL, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
LPCTSTR idfpy_script_name = TEXT("idf.py");
|
||||
|
||||
/* Get IDF_PATH */
|
||||
TCHAR idf_path[LINESIZE] = {};
|
||||
if (GetEnvironmentVariable(TEXT("IDF_PATH"), idf_path, sizeof(idf_path)) == 0) {
|
||||
DWORD err = GetLastError();
|
||||
if (err == ERROR_ENVVAR_NOT_FOUND) {
|
||||
fail("IDF_PATH environment variable needs to be set to use this tool\n");
|
||||
} else {
|
||||
fail("Unknown error (%u)\n", err);
|
||||
}
|
||||
}
|
||||
|
||||
/* Prepare the command line: python.exe %IDF_PATH%\\tools\idf.py <rest of the args> */
|
||||
TCHAR cmdline[LINESIZE] = {};
|
||||
StringCchCat(cmdline, sizeof(cmdline), TEXT("python.exe "));
|
||||
StringCchCat(cmdline, sizeof(cmdline), idf_path);
|
||||
StringCchCat(cmdline, sizeof(cmdline), TEXT("\\tools\\"));
|
||||
StringCchCat(cmdline, sizeof(cmdline), idfpy_script_name);
|
||||
StringCchCat(cmdline, sizeof(cmdline), TEXT(" "));
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
StringCchCat(cmdline, sizeof(cmdline), argv[i]);
|
||||
StringCchCat(cmdline, sizeof(cmdline), TEXT(" "));
|
||||
}
|
||||
|
||||
SetEnvironmentVariable(TEXT("IDF_PY_PROGRAM_NAME"), idfpy_script_name);
|
||||
|
||||
/* Reuse the standard streams of this process */
|
||||
STARTUPINFO start_info = {
|
||||
.cb = sizeof(STARTUPINFO),
|
||||
.hStdError = GetStdHandle(STD_ERROR_HANDLE),
|
||||
.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE),
|
||||
.hStdInput = GetStdHandle(STD_INPUT_HANDLE),
|
||||
.dwFlags = STARTF_USESTDHANDLES
|
||||
};
|
||||
|
||||
/* Run the child process */
|
||||
PROCESS_INFORMATION child_process;
|
||||
if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &start_info, &child_process)) {
|
||||
DWORD err = GetLastError();
|
||||
if (err == ERROR_FILE_NOT_FOUND) {
|
||||
fail("Can not find Python\n");
|
||||
} else {
|
||||
fail("Unknown error (%u)\n", err);
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait for it to complete */
|
||||
WaitForSingleObject(child_process.hProcess, INFINITE);
|
||||
|
||||
/* Return with the exit code of the child process */
|
||||
DWORD exitcode;
|
||||
if (!GetExitCodeProcess(child_process.hProcess, &exitcode)) {
|
||||
fail("Couldn't get the exit code (%u)\n", GetLastError());
|
||||
}
|
||||
return exitcode;
|
||||
}
|
7
tools/windows/idf_exe/toolchain-i686-w64-mingw32.cmake
Normal file
7
tools/windows/idf_exe/toolchain-i686-w64-mingw32.cmake
Normal file
|
@ -0,0 +1,7 @@
|
|||
set(CMAKE_SYSTEM_NAME Windows)
|
||||
set(CMAKE_SYSTEM_PROCESSOR x86)
|
||||
set(CMAKE_C_COMPILER i686-w64-mingw32-gcc)
|
||||
set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
Loading…
Reference in a new issue