Add CMake and Windows Visual Studio build

This commit is contained in:
pvn 2023-05-20 03:31:35 -04:00
parent f5fa34d6fe
commit 770cc8bcce
7 changed files with 185 additions and 0 deletions

78
CMakeLists.txt Normal file
View File

@ -0,0 +1,78 @@
if (MSVC)
cmake_minimum_required(VERSION 3.26)
else()
cmake_minimum_required(VERSION 3.15)
endif()
set(CMAKE_BUILD_TYPE Debug)
project (nostril C)
include_directories(${CMAKE_SOURCE_DIR}/ext/secp256k1/include)
add_subdirectory(${CMAKE_SOURCE_DIR}/ext/secp256k1)
#//////////////////////////
#windows.h header file has macros for min and max, nostril defines max
#//////////////////////////
if (MSVC)
add_definitions(-DNOMINMAX)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
endif()
#//////////////////////////
#sources
#//////////////////////////
set(src ${src})
set(src ${src} hex.h)
set(src ${src} proof.h)
set(src ${src} cursor.h)
set(src ${src} endian.h)
set(src ${src} random.h)
set(src ${src} sha256.h)
set(src ${src} sha256.c)
set(src ${src} base64.h)
set(src ${src} base64.c)
set(src ${src} aes.h)
set(src ${src} aes.c)
if (MSVC)
set(src ${src} clock_gettime.h)
endif()
#//////////////////////////
#link with libraries
#lib_dep contains a cascade definition of all the libraries needed to link
#//////////////////////////
set(lib_dep ${lib_dep})
if (MSVC)
set(lib_dep ${lib_dep} ${CMAKE_BINARY_DIR}/ext/secp256k1/src/Debug/libsecp256k1.lib)
set(lib_dep ${lib_dep} Bcrypt.lib)
else()
set(lib_dep ${lib_dep} ${CMAKE_BINARY_DIR}/ext/secp256k1/src/libsecp256k1.a)
endif()
#//////////////////////////
#executables
#//////////////////////////
# tool to generate file 'config.h' , generate from a shell with
# configurator.exe > config.h
add_executable(configurator configurator.c)
add_executable(nostril ${src} nostril.c)
target_link_libraries (nostril ${lib_dep})
#//////////////////////////
# generate config.h
#//////////////////////////
add_custom_command(
TARGET configurator
POST_BUILD
COMMAND configurator > config.h
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR}/config.h" ${CMAKE_SOURCE_DIR}
COMMENT "generating config.h"
)

View File

@ -6,6 +6,18 @@
#include <stdbool.h>
#include <sys/types.h>
#if defined(_MSC_VER)
#ifndef _SSIZE_T_DEFINED
#ifdef _WIN64
typedef unsigned __int64 ssize_t;
#else
typedef _W64 unsigned int ssize_t;
#endif
#define _SSIZE_T_DEFINED
#endif
#endif
/**
* base64_maps_t - structure to hold maps for encode/decode
*/

26
build.cmake.sh Normal file
View File

@ -0,0 +1,26 @@
#!/bin/bash
mkdir -p build
pushd build
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
cmake ..
cmake --build . --target configurator
pushd ext
pushd secp256k1
make
popd
popd
elif [[ "$OSTYPE" == "darwin"* ]]; then
cmake ..
pushd ext
pushd secp256k1
make
popd
popd
elif [[ "$OSTYPE" == "msys" ]]; then
cmake .. --fresh
fi
sleep 1
cmake --build .
popd
exit

39
clock_gettime.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef CLOCK_GETTIME_H_
#define CLOCK_GETTIME_H_
#include <windows.h>
#define MS_PER_SEC 1000ULL // MS = milliseconds
#define US_PER_MS 1000ULL // US = microseconds
#define HNS_PER_US 10ULL // HNS = hundred-nanoseconds (e.g., 1 hns = 100 ns)
#define NS_PER_US 1000ULL
#define HNS_PER_SEC (MS_PER_SEC * US_PER_MS * HNS_PER_US)
#define NS_PER_HNS (100ULL) // NS = nanoseconds
#define NS_PER_SEC (MS_PER_SEC * US_PER_MS * NS_PER_US)
int clock_gettime(int X, struct timespec* tv)
{
static LARGE_INTEGER ticksPerSec;
LARGE_INTEGER ticks;
if (!ticksPerSec.QuadPart)
{
QueryPerformanceFrequency(&ticksPerSec);
if (!ticksPerSec.QuadPart)
{
errno = ENOTSUP;
return -1;
}
}
QueryPerformanceCounter(&ticks);
tv->tv_sec = (long)(ticks.QuadPart / ticksPerSec.QuadPart);
tv->tv_nsec = (long)(((ticks.QuadPart % ticksPerSec.QuadPart) * NS_PER_SEC) / ticksPerSec.QuadPart);
return 0;
}
#endif

View File

@ -37,7 +37,11 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#define sleep Sleep
#else
#include <unistd.h>
#endif
#ifdef _MSC_VER
#define popen _popen

View File

@ -6,8 +6,19 @@
#include <assert.h>
#include <string.h>
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4005)
# pragma warning(disable: 4477)
#endif
#ifdef _MSC_VER
#define unlikely
#define likely
#else
#define unlikely(x) __builtin_expect((x),0)
#define likely(x) __builtin_expect((x),1)
#endif
struct cursor {
unsigned char *start;

View File

@ -5,7 +5,15 @@
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#ifdef _MSC_VER
#else
#include <unistd.h>
#endif
#ifdef _MSC_VER
#include "clock_gettime.h"
#define CLOCK_MONOTONIC 0
#endif
#include "secp256k1.h"
#include "secp256k1_ecdh.h"
@ -639,7 +647,11 @@ static int make_encrypted_dm(secp256k1_context *ctx, struct key *key,
unsigned char iv[16];
unsigned char compressed_pubkey[33];
int content_len = strlen(ev->content);
#ifdef _MSC_VER
unsigned char* encbuf = malloc(content_len + (content_len % 16) + 1);
#else
unsigned char encbuf[content_len + (content_len % 16) + 1];
#endif
struct cursor cur;
secp256k1_pubkey pubkey;
@ -714,6 +726,9 @@ static int make_encrypted_dm(secp256k1_context *ctx, struct key *key,
cur.p += 65;
#ifdef _MSC_VER
free(encbuf);
#endif
return 1;
}