Skip to content

Commit

Permalink
Stop the sulk with GPL2016 on Windows (#602)
Browse files Browse the repository at this point in the history
* Fixes for VS2017.
* Added a getopt implementation.
* Added a unistd.h.
* Corrected closing of sockets for Windows versions.
* Fixed gdbserver CMakeLists.txt to play nice with VS2017.
* Fixed include of getopt.h and unistd.h for WIN32.
* Added a unistd.h.
* Corrected closing of sockets for Windows versions.
* Fixed gdbserver CMakeLists.txt to play nice with VS2017.
* Override /MD to /MT for MSVC to match libusb's builds.
* MSVC settings should be if (MSVC).
* Don't busy-wait for long periods in usleep().
* Dynamic link to MSVC libusb binaries.
* Added Visual Studio section to compiling.md.
* Added -D_CRT_NONSTDC_NO_WARNINGS to MSVC flags.
* Prevented some more warnings under MSVC.
  • Loading branch information
orinem authored and xor-gate committed Jun 17, 2017
1 parent 55c0572 commit 79d9178
Show file tree
Hide file tree
Showing 20 changed files with 515 additions and 35 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 2.8.7)
set(CMAKE_USER_MAKE_RULES_OVERRIDE cmake/c_flag_overrides.cmake)
project(stlink C)
set(PROJECT_DESCRIPTION "Open source version of the STMicroelectronics Stlink Tools")
set(STLINK_UDEV_RULES_DIR "/etc/udev/rules.d" CACHE PATH "Udev rules directory")
Expand Down Expand Up @@ -71,6 +72,12 @@ include_directories(${LIBUSB_INCLUDE_DIR})
include_directories(include)
include_directories(${PROJECT_BINARY_DIR}/include)
include_directories(src/mingw)
if (MSVC)
include_directories(src/win32)
include_directories(src/getopt)
# Use string.h rather than strings.h and disable annoying warnings
add_definitions(-DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS /wd4710)
endif ()

###
# Shared library
Expand Down
7 changes: 7 additions & 0 deletions cmake/c_flag_overrides.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if(MSVC)
message(STATUS "MSVC C Flags override to /MT")
set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1")
set(CMAKE_C_FLAGS_MINSIZEREL_INIT "/MT /O1 /Ob1 /D NDEBUG")
set(CMAKE_C_FLAGS_RELEASE_INIT "/MT /O2 /Ob2 /D NDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG")
endif()
4 changes: 2 additions & 2 deletions cmake/modules/FindLibUSB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ if (MSYS OR MINGW)
elseif(CMAKE_VS_PLATFORM_NAME)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
find_library(LIBUSB_LIBRARY NAMES ${LIBUSB_NAME}
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MS64/static)
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MS64/dll)
else ()
find_library(LIBUSB_LIBRARY NAMES ${LIBUSB_NAME}
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MS32/static)
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MS32/dll)
endif ()
else()
find_library(LIBUSB_LIBRARY NAMES ${LIBUSB_NAME}
Expand Down
31 changes: 31 additions & 0 deletions doc/compiling.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,34 @@ Check and execute (in the script folder) `<source-dir>\scripts\mingw64-build.bat

NOTE: when installing different toolchains make sure you edit the path in the `mingw64-build.bat`
the build script uses currently `C:\Program Files\mingw-w64\x86_64-5.3.0-win32-sjlj-rt_v4-rev0\mingw64\bin`

## Windows (Visual Studio)

### Prerequisites

* 7Zip
* CMake (tested with version 3.9.0-rc2)
* Visual Studio 2017 Community (other versions will likely work but are untested; the Community edition is free for open source
development)

### Installation

1. Install 7Zip from <http://www.7-zip.org>
2. Install CMake from <https://cmake.org/download>
3. Git clone or download stlink sourcefiles zip

### Building

These instructions are for a 32bit version.

In a command prompt, change directory to the folder where the stlink files were cloned (or unzipped).
Make sure the build folder exists (`mkdir build` if not).
From the build folder, run cmake (`cd build; cmake ..`).

This will create a solution (stlink.sln) in the build folder. Open it in Visual Studio, select the Solution Configuration (Debug or
Release) and build the solution normally (F7).

NOTES: This solution will link to the dll version of libusb-1.0. To debug or run the executable, the dll version of libusb-1.0 must
be either on the path, or in the same folder as the executable. It can be copied from here:
`build\3thparty\libusb-1.0.21\MS32\dll\libusb-1.0.dll`.

8 changes: 8 additions & 0 deletions include/stlink/sg.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
#ifndef STLINK_SG_H
#define STLINK_SG_H

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4200 4255 4668 4820)
#include <libusb.h>
#pragma warning(pop)
#else
#include <libusb.h>
#endif

#include "stlink.h"

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion include/stlink/tools/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct flash_opts
size_t flash_size; /* --flash=n[k][m] */
};

#define FLASH_OPTS_INITIALIZER {0, NULL, {}, NULL, 0, 0, 0, 0, 0, 0 }
#define FLASH_OPTS_INITIALIZER {0, NULL, { 0 }, NULL, 0, 0, 0, 0, 0, 0 }

int flash_get_opts(struct flash_opts* o, int ac, char** av);

Expand Down
3 changes: 3 additions & 0 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#ifndef _WIN32
#define O_BINARY 0 //! @todo get rid of this OH MY (@xor-gate)
#endif
#ifdef _MSC_VER
#define __attribute__(x)
#endif

/* todo: stm32l15xxx flash memory, pm0062 manual */

Expand Down
21 changes: 15 additions & 6 deletions src/gdbserver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
add_executable(st-util gdb-remote.c
gdb-remote.h
gdb-server.c
gdb-server.h
semihosting.c
semihosting.h)
set(STUTIL_SOURCE
gdb-remote.c
gdb-remote.h
gdb-server.c
gdb-server.h
semihosting.c
semihosting.h)

if (MSVC)
# We need a getopt from somewhere...
set(STUTIL_SOURCE "${STUTIL_SOURCE};../getopt/getopt.c")

This comment has been minimized.

Copy link
@rpavlik

rpavlik Mar 16, 2018

This can actually be

list(APPEND STUTIL_SOURCE "../getopt/getopt.c")

Marginally faster, as well as clearer.

This comment has been minimized.

Copy link
@xor-gate

xor-gate Mar 17, 2018

Member

I think the list append is available since cmake 3.0 but not sure. Thats why we used set if I remember correctly. We could make cmake 3.0 required or leave it just as is.

This comment has been minimized.

Copy link
@rpavlik

rpavlik via email Mar 17, 2018

endif()

add_executable(st-util ${STUTIL_SOURCE})

if (WIN32 OR APPLE)
target_link_libraries(st-util ${STLINK_LIB_STATIC})
else()
Expand Down
2 changes: 1 addition & 1 deletion src/gdbserver/gdb-remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef __MINGW32__
#if defined(__MINGW32__) || defined(_MSC_VER)
#include <mingw.h>
#else
#include <unistd.h>
Expand Down
32 changes: 20 additions & 12 deletions src/gdbserver/gdb-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef __MINGW32__
#if defined(_MSC_VER)
#include <stdbool.h>
#define __attribute__(x)
#endif
#if defined(__MINGW32__) || defined(_MSC_VER)
#include <mingw.h>
#else
#include <unistd.h>
Expand Down Expand Up @@ -239,7 +243,7 @@ int main(int argc, char** argv) {
sl->verbose=0;
current_memory_map = make_memory_map(sl);

#ifdef __MINGW32__
#if defined(__MINGW32__) || defined(_MSC_VER)

This comment has been minimized.

Copy link
@rpavlik

rpavlik Mar 16, 2018

For these, does it make more sense to check

#ifdef _WIN32

?

That's set by all compilers on win32 and win64.

This comment has been minimized.

Copy link
@xor-gate

xor-gate Mar 17, 2018

Member

Yes you are right, feel free to submit a PR for this improvement

WSADATA wsadata;
if (WSAStartup(MAKEWORD(2,2),&wsadata) !=0 ) {
goto winsock_error;
Expand All @@ -260,7 +264,7 @@ int main(int argc, char** argv) {
stlink_run(sl);
} while (state.persistent);

#ifdef __MINGW32__
#if defined(__MINGW32__) || defined(_MSC_VER)
winsock_error:
WSACleanup();
#endif
Expand Down Expand Up @@ -1061,7 +1065,11 @@ int serve(stlink_t *sl, st_state_t *st) {
return 1;
}

close(sock);
#if defined(__MINGW32__) || defined(_MSC_VER)
win32_close_socket(sock);
#else
close(sock);
#endif

stlink_force_debug(sl);
if (st->reset) {
Expand All @@ -1084,8 +1092,8 @@ int serve(stlink_t *sl, st_state_t *st) {
int status = gdb_recv_packet(client, &packet);
if(status < 0) {
ELOG("cannot recv: %d\n", status);
#ifdef __MINGW32__
win32_close_socket(sock);
#if defined(__MINGW32__) || defined(_MSC_VER)
win32_close_socket(client);
#endif
return 1;
}
Expand Down Expand Up @@ -1337,8 +1345,8 @@ int serve(stlink_t *sl, st_state_t *st) {
status = gdb_check_for_interrupt(client);
if(status < 0) {
ELOG("cannot check for int: %d\n", status);
#ifdef __MINGW32__
win32_close_socket(sock);
#if defined(__MINGW32__) || defined(_MSC_VER)
win32_close_socket(client);
#endif
return 1;
}
Expand Down Expand Up @@ -1739,8 +1747,8 @@ int serve(stlink_t *sl, st_state_t *st) {
ELOG("cannot send: %d\n", result);
free(reply);
free(packet);
#ifdef __MINGW32__
win32_close_socket(sock);
#if defined(__MINGW32__) || defined(_MSC_VER)
win32_close_socket(client);
#endif
return 1;
}
Expand All @@ -1751,8 +1759,8 @@ int serve(stlink_t *sl, st_state_t *st) {
free(packet);
}

#ifdef __MINGW32__
win32_close_socket(sock);
#if defined(__MINGW32__) || defined(_MSC_VER)
win32_close_socket(client);
#endif

return 0;
Expand Down
24 changes: 24 additions & 0 deletions src/getopt/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2012, Kim Gräsman
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Kim Gräsman nor the
names of contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL KIM GRÄSMAN BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 changes: 21 additions & 0 deletions src/getopt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
getopt_port
===========

[Kim Gräsman](http://grundlig.wordpress.com)
[@kimgr](http://twitter.com/kimgr)

An original implementation of `getopt` and `getopt_long` with limited GNU extensions. Provided under the BSD license, to allow non-GPL projects to use `getopt`-style command-line parsing.

So far only built with Visual C++, but has no inherently non-portable constructs.

Intended to be embedded into your code tree -- `getopt.h` and `getopt.c` are self-contained and should work in any context.

Comes with a reasonable unit test suite.

See also:

* [Full Win32 getopt port](http://www.codeproject.com/Articles/157001/Full-getopt-Port-for-Unicode-and-Multibyte-Microso) -- LGPL licensed.
* [XGetOpt](http://www.codeproject.com/Articles/1940/XGetopt-A-Unix-compatible-getopt-for-MFC-and-Win32) -- No `getopt_long` support.
* [Free Getopt](https://sourceforge.net/projects/freegetopt/) -- No `getopt_long` support.

For license terms, see LICENSE.txt.
Loading

0 comments on commit 79d9178

Please sign in to comment.