Skip to content

Commit

Permalink
Merge pull request #1421 from runelauridsen/windows-relative-chip-path
Browse files Browse the repository at this point in the history
Make path to .chip files relative to installation directory on Windows
  • Loading branch information
Nightwalker-87 authored Oct 3, 2024
2 parents 25785b9 + 4990b66 commit 275d7eb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build
build-mingw
build-mingw-32
build-mingw-64

Expand Down
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ if (STLINK_HAVE_UNISTD_H)
add_definitions(-DSTLINK_HAVE_UNISTD_H)
endif()

CHECK_INCLUDE_FILE(dirent.h STLINK_HAVE_DIRENT_H)
if (STLINK_HAVE_DIRENT_H)
add_definitions(-DSTLINK_HAVE_DIRENT_H)
if (NOT WIN32) # Use GetModuleFileNameA and FindFirstFileA on Windows
CHECK_INCLUDE_FILE(dirent.h STLINK_HAVE_DIRENT_H)
if (STLINK_HAVE_DIRENT_H)
add_definitions(-DSTLINK_HAVE_DIRENT_H)
endif()
endif()

if (MSVC)
Expand Down Expand Up @@ -380,7 +382,7 @@ endif()

# MCU configuration files
if (WIN32)
set(CMAKE_CHIPS_DIR ${CMAKE_INSTALL_PREFIX}/config/chips)
set(CMAKE_CHIPS_DIR ./config/chips)
else ()
set(CMAKE_CHIPS_DIR ${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/chips)
endif ()
Expand Down
32 changes: 27 additions & 5 deletions src/stlink-lib/chipid.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,35 @@ void init_chipids(char *dir_to_scan) {
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATAA ffd;
char filepath[MAX_PATH] = {0};
DWORD filepathlen;
int numslash;
StringCchCopyA(filepath, STLINK_ARRAY_SIZE(filepath), dir_to_scan);

if (FAILED(
StringCchCatA(filepath, STLINK_ARRAY_SIZE(filepath), "\\*.chip"))) {
ELOG("Path to chips's dir too long.\n");
filepathlen = GetModuleFileNameA(NULL, filepath, STLINK_ARRAY_SIZE(filepath));
if (filepathlen == 0) {
ELOG("GetModuleFileNameA failed: %u\n", (unsigned)GetLastError());
return;
}

// Chop off exe and bin directory to get installation directory
// 'C:\path-to-stlink\bin\st-util.exe' -> 'C:\path-to-stlink'
numslash = 2;
while (filepathlen > 0 && numslash > 0) {
if (filepath[filepathlen - 1] == '\\') {
numslash--;
}
filepathlen--;
}
if (filepathlen <= 0) {
ELOG("GetModuleFileNameA returned an invalid path: %s\n", filepath);
return;
}

filepath[filepathlen] = '\0';
StringCchCatA(filepath, STLINK_ARRAY_SIZE(filepath), "\\");
StringCchCatA(filepath, STLINK_ARRAY_SIZE(filepath), dir_to_scan);
StringCchCatA(filepath, STLINK_ARRAY_SIZE(filepath), "\\*.chip");

hFind = FindFirstFileA(filepath, &ffd);

if (INVALID_HANDLE_VALUE == hFind) {
Expand All @@ -266,8 +287,9 @@ void init_chipids(char *dir_to_scan) {
}

do {
memset(filepath, 0, STLINK_ARRAY_SIZE(filepath));
StringCchCopyA(filepath, STLINK_ARRAY_SIZE(filepath), dir_to_scan);
filepath[filepathlen] = '\0';
StringCchCatA(filepath, STLINK_ARRAY_SIZE(filepath), "\\");
StringCchCatA(filepath, STLINK_ARRAY_SIZE(filepath), dir_to_scan);
StringCchCatA(filepath, STLINK_ARRAY_SIZE(filepath), "\\");
StringCchCatA(filepath, STLINK_ARRAY_SIZE(filepath), ffd.cFileName);
process_chipfile(filepath);
Expand Down

0 comments on commit 275d7eb

Please sign in to comment.