Code generate secp256k1 modules into header files

This commit is contained in:
doyle 2022-01-24 00:41:51 +11:00
parent 3a9c1dc4bf
commit 2efe69b68e
8 changed files with 4777 additions and 216 deletions

View File

@ -29,7 +29,6 @@ definitive explanations).
#define ECMULT_GEN_PREC_BITS 4 #define ECMULT_GEN_PREC_BITS 4
#define ECMULT_WINDOW_SIZE 15 #define ECMULT_WINDOW_SIZE 15
#include "bt_secp256k1_i64.h" #include "bt_secp256k1_i64.h"
#include <stdio.h>
``` ```
## Details ## Details

View File

@ -4,6 +4,10 @@
#define BT_SECP256K1_IMPLEMENTATION #define BT_SECP256K1_IMPLEMENTATION
// NOTE: The 2 following defines are optional! We provide a default value if // NOTE: The 2 following defines are optional! We provide a default value if
// these are not defined before the implementation // these are not defined before the implementation
#define ENABLE_MODULE_ECDH
#define ENABLE_MODULE_RECOVERY
#define ENABLE_MODULE_EXTRAKEYS
#define ENABLE_MODULE_SCHNORRSIG
#define ECMULT_GEN_PREC_BITS 4 #define ECMULT_GEN_PREC_BITS 4
#define ECMULT_WINDOW_SIZE 15 #define ECMULT_WINDOW_SIZE 15

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -46,11 +46,11 @@ struct StringList { String string; StringList *next; };
struct LoadedFile struct LoadedFile
{ {
unsigned flag; unsigned flag; // Corresponds to FileFlag enum
char *buffer; String buffer; // The contents of the file loaded from disk
int size; String sub_dir; // The sub directory that the file orginated from
String sub_dir; String name; // The file name
String name; String module_macro_name; // For secp256k1 modules, the name of the C macro used to identify the file
}; };
FILE_SCOPE bool LoadFiles(LoadedFile *files, int file_count, char const *root_dir) FILE_SCOPE bool LoadFiles(LoadedFile *files, int file_count, char const *root_dir)
@ -69,8 +69,8 @@ FILE_SCOPE bool LoadFiles(LoadedFile *files, int file_count, char const *root_di
snprintf(file_path, sizeof(file_path), "%s/%.*s", root_dir, file->name.size, file->name.str); snprintf(file_path, sizeof(file_path), "%s/%.*s", root_dir, file->name.size, file->name.str);
} }
file->buffer = ReadEntireFile(file_path, &file->size); file->buffer.str = ReadEntireFile(file_path, &file->buffer.size);
if (!file->buffer || file->size == 0) if (!file->buffer.str || file->buffer.size == 0)
{ {
fprintf(stderr, "Failed to load file [file=%s]\n", file_path); fprintf(stderr, "Failed to load file [file=%s]\n", file_path);
result = false; result = false;
@ -104,6 +104,17 @@ FILE_SCOPE StringList *StringListAppendRef(StringList *list, String string)
return result; return result;
} }
FILE_SCOPE StringList *StringListAppendCopy(StringList *list, String string)
{
StringList *result = list;
if (string.str && string.size)
{
String copy = StringCopy(string);
result = StringListAppendRef(result, copy);
}
return result;
}
FILE_SCOPE String StringListBuild(StringList *list) FILE_SCOPE String StringListBuild(StringList *list)
{ {
int total_size = 0; int total_size = 0;
@ -211,7 +222,10 @@ FILE_SCOPE String StringReplace(String src, String find, String replace)
StringList *AddSourceFileComment(StringList *tail, LoadedFile const *file) StringList *AddSourceFileComment(StringList *tail, LoadedFile const *file)
{ {
StringList *result = StringListAppendRef(tail, STRING("\n// File: ")); StringList *result = StringListAppendRef(tail, STRING(
"// -----------------------------------------------------------------------------\n"
"// File: "
));
if (file->sub_dir.size) if (file->sub_dir.size)
{ {
result = StringListAppendRef(result, file->sub_dir); result = StringListAppendRef(result, file->sub_dir);
@ -220,6 +234,7 @@ StringList *AddSourceFileComment(StringList *tail, LoadedFile const *file)
result = StringListAppendRef(result, file->name); result = StringListAppendRef(result, file->name);
result = StringListAppendRef(result, STRING("\n")); result = StringListAppendRef(result, STRING("\n"));
result = StringListAppendRef(result, STRING("// -----------------------------------------------------------------------------\n"));
return result; return result;
} }
@ -232,61 +247,76 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
// NOTE: Load files into memory
LoadedFile intro_files[] = { LoadedFile intro_files[] = {
{FileFlag_Normal, nullptr, 0, STRING(""), STRING("COPYING")}, {FileFlag_Normal, nullptr, 0, STRING(""), STRING("COPYING"), STRING("")},
}; };
LoadedFile header_files[] = { LoadedFile header_files[] = {
{FileFlag_Normal, nullptr, 0, STRING("include"), STRING("secp256k1.h")}, {FileFlag_Normal, nullptr, 0, STRING("include"), STRING("secp256k1.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("include"), STRING("secp256k1_preallocated.h")}, {FileFlag_Normal, nullptr, 0, STRING("include"), STRING("secp256k1_preallocated.h"), STRING("")},
};
LoadedFile extra_module_header_files[] = {
{FileFlag_Normal, nullptr, 0, STRING("include"), STRING("secp256k1_ecdh.h"), STRING("ECDH")},
{FileFlag_Normal, nullptr, 0, STRING("include"), STRING("secp256k1_recovery.h"), STRING("RECOVERY")},
{FileFlag_Normal, nullptr, 0, STRING("include"), STRING("secp256k1_extrakeys.h"), STRING("EXTRAKEYS")},
{FileFlag_Normal, nullptr, 0, STRING("include"), STRING("secp256k1_schnorrsig.h"), STRING("SCHNORRSIG")},
}; };
LoadedFile private_header_files[] = { LoadedFile private_header_files[] = {
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("util.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("util.h"), STRING("")},
{FileFlag_Mul64Support, nullptr, 0, STRING("src"), STRING("field_10x26.h")}, {FileFlag_Mul64Support, nullptr, 0, STRING("src"), STRING("field_10x26.h"), STRING("")},
{FileFlag_Mul64Support, nullptr, 0, STRING("src"), STRING("scalar_8x32.h")}, {FileFlag_Mul64Support, nullptr, 0, STRING("src"), STRING("scalar_8x32.h"), STRING("")},
{FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("field_5x52.h")}, {FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("field_5x52.h"), STRING("")},
{FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("scalar_4x64.h")}, {FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("scalar_4x64.h"), STRING("")},
{FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("modinv64.h")}, {FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("modinv64.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("hash.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("hash.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("modinv32.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("modinv32.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("assumptions.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("assumptions.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("scalar.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("scalar.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("field.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("field.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("group.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("group.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult_const.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult_const.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult_gen.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult_gen.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("scratch.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("scratch.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("precomputed_ecmult.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("precomputed_ecmult.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("precomputed_ecmult_gen.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("precomputed_ecmult_gen.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecdsa.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecdsa.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("eckey.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("eckey.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("selftest.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("selftest.h"), STRING("")},
}; };
LoadedFile private_impl_files[] = { LoadedFile private_impl_files[] = {
{FileFlag_Mul64Support, nullptr, 0, STRING("src"), STRING("field_10x26_impl.h")}, {FileFlag_Mul64Support, nullptr, 0, STRING("src"), STRING("field_10x26_impl.h"), STRING("")},
{FileFlag_Mul64Support, nullptr, 0, STRING("src"), STRING("scalar_8x32_impl.h")}, {FileFlag_Mul64Support, nullptr, 0, STRING("src"), STRING("scalar_8x32_impl.h"), STRING("")},
{FileFlag_AsmInt128, nullptr, 0, STRING("src"), STRING("field_5x52_int128_impl.h")}, {FileFlag_AsmInt128, nullptr, 0, STRING("src"), STRING("field_5x52_int128_impl.h"), STRING("")},
{FileFlag_AsmX86_64, nullptr, 0, STRING("src"), STRING("field_5x52_asm_impl.h")}, {FileFlag_AsmX86_64, nullptr, 0, STRING("src"), STRING("field_5x52_asm_impl.h"), STRING("")},
{FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("field_5x52_impl.h")}, {FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("field_5x52_impl.h"), STRING("")},
{FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("scalar_4x64_impl.h")}, {FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("scalar_4x64_impl.h"), STRING("")},
{FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("modinv64_impl.h")}, {FileFlag_Mul128Support, nullptr, 0, STRING("src"), STRING("modinv64_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecdsa_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecdsa_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("eckey_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("eckey_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("group_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("group_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("field_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("field_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("scalar_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("scalar_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult_const_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult_const_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult_gen_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("ecmult_gen_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("precomputed_ecmult.c")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("precomputed_ecmult.c"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("precomputed_ecmult_gen.c")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("precomputed_ecmult_gen.c"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("hash_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("hash_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("modinv32_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("modinv32_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("scratch_impl.h")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("scratch_impl.h"), STRING("")},
{FileFlag_Normal, nullptr, 0, STRING("src"), STRING("secp256k1.c")}, {FileFlag_Normal, nullptr, 0, STRING("src"), STRING("secp256k1.c"), STRING("")},
};
LoadedFile private_extra_module_impl_files[] = {
{FileFlag_Normal, nullptr, 0, STRING("src/modules/ecdh"), STRING("main_impl.h"), STRING("ECDH")},
{FileFlag_Normal, nullptr, 0, STRING("src/modules/recovery"), STRING("main_impl.h"), STRING("RECOVERY")},
{FileFlag_Normal, nullptr, 0, STRING("src/modules/extrakeys"), STRING("main_impl.h"), STRING("EXTRAKEYS")},
{FileFlag_Normal, nullptr, 0, STRING("src/modules/schnorrsig"), STRING("main_impl.h"), STRING("SCHNORRSIG")},
}; };
#define ARRAY_COUNT(array) sizeof(array)/sizeof((array)[0]) #define ARRAY_COUNT(array) sizeof(array)/sizeof((array)[0])
@ -294,18 +324,62 @@ int main(int argc, char *argv[])
bool files_loaded = true; bool files_loaded = true;
files_loaded &= LoadFiles(intro_files, ARRAY_COUNT(intro_files), root_dir); files_loaded &= LoadFiles(intro_files, ARRAY_COUNT(intro_files), root_dir);
files_loaded &= LoadFiles(header_files, ARRAY_COUNT(header_files), root_dir); files_loaded &= LoadFiles(header_files, ARRAY_COUNT(header_files), root_dir);
files_loaded &= LoadFiles(extra_module_header_files, ARRAY_COUNT(extra_module_header_files), root_dir);
files_loaded &= LoadFiles(private_header_files, ARRAY_COUNT(private_header_files), root_dir); files_loaded &= LoadFiles(private_header_files, ARRAY_COUNT(private_header_files), root_dir);
files_loaded &= LoadFiles(private_impl_files, ARRAY_COUNT(private_impl_files), root_dir); files_loaded &= LoadFiles(private_impl_files, ARRAY_COUNT(private_impl_files), root_dir);
files_loaded &= LoadFiles(private_extra_module_impl_files, ARRAY_COUNT(private_extra_module_impl_files), root_dir);
if (!files_loaded) if (!files_loaded)
return -1; return -1;
// NOTE: Patch problematic source code before code generation
{
for (LoadedFile &file : private_header_files)
{
if (StringEquals(file.name, STRING("util.h")))
{
// NOTE: Fix implicit cast from void to unsigned char C++ error
file.buffer = StringReplace(
file.buffer,
STRING("const unsigned char *p1 = s1, *p2 = s2;"),
STRING("const unsigned char *p1 = (const unsigned char *)s1, *p2 = (const unsigned char *)s2;"));
break;
}
}
for (LoadedFile &file : private_impl_files)
{
if (StringEquals(file.name, STRING("secp256k1.c")))
{
// NOTE: Patch out module includes, we insert them manually after
// the main implementation file
file.buffer = StringFindThenInsert(file.buffer, STRING("# include \"modules"), STRING("// "));
break;
}
}
for (LoadedFile &file : private_extra_module_impl_files)
{
// NOTE: modules all use relative headers, so patch them out early.
file.buffer = StringFindThenInsert(file.buffer, STRING("#include"), STRING("// "));
if (StringEquals(file.sub_dir, STRING("src/modules/schnorrsig")) && StringEquals(file.name, STRING("main_impl.h")))
{
// NOTE: Fix implicit cast from void to unsigned char C++ error
file.buffer = StringReplace(file.buffer,
STRING("secp256k1_sha256_write(&sha, data, 32)"),
STRING("secp256k1_sha256_write(&sha, (const unsigned char *)data, 32)"));
}
}
}
unsigned const BUILD_FLAGS[] = { unsigned const BUILD_FLAGS[] = {
FileFlag_Normal | FileFlag_Mul64Support, FileFlag_Normal | FileFlag_Mul64Support,
FileFlag_Normal | FileFlag_Mul128Support | FileFlag_AsmInt128, FileFlag_Normal | FileFlag_Mul128Support | FileFlag_AsmInt128,
FileFlag_Normal | FileFlag_Mul128Support | FileFlag_AsmX86_64, FileFlag_Normal | FileFlag_Mul128Support | FileFlag_AsmX86_64,
}; };
// NOTE: Code generate
for (int build_index = 0; build_index < ARRAY_COUNT(BUILD_FLAGS); build_index++) for (int build_index = 0; build_index < ARRAY_COUNT(BUILD_FLAGS); build_index++)
{ {
unsigned const build_flags = BUILD_FLAGS[build_index]; unsigned const build_flags = BUILD_FLAGS[build_index];
@ -362,7 +436,7 @@ int main(int argc, char *argv[])
for (LoadedFile const &file : intro_files) for (LoadedFile const &file : intro_files)
{ {
if (build_flags & file.flag) if (build_flags & file.flag)
list_tail = StringListAppendRef(list_tail, String{file.buffer, file.size}); list_tail = StringListAppendRef(list_tail, file.buffer);
} }
list_tail = StringListAppendRef(list_tail, STRING("*/\n")); list_tail = StringListAppendRef(list_tail, STRING("*/\n"));
@ -434,7 +508,7 @@ int main(int argc, char *argv[])
"#endif\n" "#endif\n"
"\n" "\n"
"// -----------------------------------------------------------------------------\n" "// -----------------------------------------------------------------------------\n"
"// NOTE: Single Header Start\n" "// NOTE: Header Start\n"
"// -----------------------------------------------------------------------------\n" "// -----------------------------------------------------------------------------\n"
)); ));
@ -456,15 +530,59 @@ int main(int argc, char *argv[])
if (build_flags & file.flag) if (build_flags & file.flag)
{ {
list_tail = AddSourceFileComment(list_tail, &file); list_tail = AddSourceFileComment(list_tail, &file);
list_tail = StringListAppendRef(list_tail, String{file.buffer, file.size}); list_tail = StringListAppendRef(list_tail, file.buffer);
list_tail = StringListAppendRef(list_tail, STRING("\n"));
} }
} }
// -------------------------------------------------------------------------------------
// Extra Module Header files
// -------------------------------------------------------------------------------------
for (LoadedFile const &file : extra_module_header_files)
{
if (build_flags & file.flag)
{
// NOTE: These are optional, conditionally defined based on
// the ENABLE_MODULE_* macro
char macro_name_buf[256];
String macro_name = {};
macro_name.str = macro_name_buf;
macro_name.size = snprintf(macro_name_buf,
sizeof(macro_name_buf),
"ENABLE_MODULE_%.*s",
file.module_macro_name.size,
file.module_macro_name.str);
list_tail = AddSourceFileComment(list_tail, &file);
// NOTE: Begin the conditional macro
list_tail = StringListAppendRef(list_tail, STRING("#if defined("));
list_tail = StringListAppendCopy(list_tail, macro_name);
list_tail = StringListAppendRef(list_tail, STRING(")\n"));
// NOTE: Write file
list_tail = StringListAppendRef(list_tail, file.buffer);
// NOTE: End the conditional macro
list_tail = StringListAppendRef(list_tail, STRING("#endif //"));
list_tail = StringListAppendCopy(list_tail, macro_name);
list_tail = StringListAppendRef(list_tail, STRING("\n\n"));
}
}
list_tail = StringListAppendRef(list_tail, STRING("#endif // BT_SECP256K1_H\n")); list_tail = StringListAppendRef(list_tail, STRING("#endif // BT_SECP256K1_H\n"));
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
// Add implementation defines // Add implementation defines
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
list_tail = StringListAppendRef(list_tail, STRING("#if defined(BT_SECP256K1_IMPLEMENTATION)\n")); list_tail = StringListAppendRef(list_tail, STRING(
"\n"
"// -----------------------------------------------------------------------------\n"
"// NOTE: Implementation Start\n"
"// -----------------------------------------------------------------------------\n"
"#if defined(BT_SECP256K1_IMPLEMENTATION)\n"
));
{ {
list_tail = StringListAppendRef(list_tail, STRING("#if !defined(ECMULT_GEN_PREC_BITS)\n")); list_tail = StringListAppendRef(list_tail, STRING("#if !defined(ECMULT_GEN_PREC_BITS)\n"));
@ -529,7 +647,7 @@ int main(int argc, char *argv[])
if (build_flags & file.flag) if (build_flags & file.flag)
{ {
list_tail = AddSourceFileComment(list_tail, &file); list_tail = AddSourceFileComment(list_tail, &file);
list_tail = StringListAppendRef(list_tail, String{file.buffer, file.size}); list_tail = StringListAppendRef(list_tail, file.buffer);
} }
} }
@ -538,7 +656,40 @@ int main(int argc, char *argv[])
if (build_flags & file.flag) if (build_flags & file.flag)
{ {
list_tail = AddSourceFileComment(list_tail, &file); list_tail = AddSourceFileComment(list_tail, &file);
list_tail = StringListAppendRef(list_tail, String{file.buffer, file.size}); list_tail = StringListAppendRef(list_tail, file.buffer);
}
}
for (LoadedFile const &file : private_extra_module_impl_files)
{
if (build_flags & file.flag)
{
// NOTE: These are optional, conditionally defined based on
// the ENABLE_MODULE_* macro
char macro_name_buf[256];
String macro_name = {};
macro_name.str = macro_name_buf;
macro_name.size = snprintf(macro_name_buf,
sizeof(macro_name_buf),
"ENABLE_MODULE_%.*s",
file.module_macro_name.size,
file.module_macro_name.str);
list_tail = AddSourceFileComment(list_tail, &file);
// NOTE: Begin the conditional macro
list_tail = StringListAppendRef(list_tail, STRING("#if defined("));
list_tail = StringListAppendCopy(list_tail, macro_name);
list_tail = StringListAppendRef(list_tail, STRING(")\n"));
// NOTE: Write file
list_tail = StringListAppendRef(list_tail, file.buffer);
// NOTE: End the conditional macro
list_tail = StringListAppendRef(list_tail, STRING("#endif //"));
list_tail = StringListAppendCopy(list_tail, macro_name);
list_tail = StringListAppendRef(list_tail, STRING("\n\n"));
} }
} }
@ -573,8 +724,10 @@ int main(int argc, char *argv[])
LoadedFileList file_list[] = { LoadedFileList file_list[] = {
{header_files, ARRAY_COUNT(header_files)}, {header_files, ARRAY_COUNT(header_files)},
{extra_module_header_files, ARRAY_COUNT(extra_module_header_files)},
{private_header_files, ARRAY_COUNT(private_header_files)}, {private_header_files, ARRAY_COUNT(private_header_files)},
{private_impl_files, ARRAY_COUNT(private_impl_files)}, {private_impl_files, ARRAY_COUNT(private_impl_files)},
{private_extra_module_impl_files, ARRAY_COUNT(private_extra_module_impl_files)},
}; };
for (LoadedFileList const &list : file_list) for (LoadedFileList const &list : file_list)
@ -605,15 +758,8 @@ int main(int argc, char *argv[])
// NOTE: Misc patches to source code // NOTE: Misc patches to source code
{ {
// NOTE: Patch one problematic line that causes a compile failure in
// C++ (but not C, since this a C library).
buffer = StringReplace(
buffer,
STRING("const unsigned char *p1 = s1, *p2 = s2;"),
STRING("const unsigned char *p1 = (const unsigned char *)s1, *p2 = (const unsigned char *)s2;"));
// NOTE: Delete any Windows style new-lines if there were any // NOTE: Delete any Windows style new-lines if there were any
buffer = StringReplace(buffer, STRING("\r"), STRING("")); // buffer = StringReplace(buffer, STRING("\r"), STRING(""));
} }
// NOTE: Output file // NOTE: Output file

View File

@ -15,7 +15,7 @@ set common_linker_flags=-link -nologo
REM Build the metaprogram REM Build the metaprogram
pushd !build_dir! pushd !build_dir!
cl !common_compiler_flags! !code_dir!\bt_secp256k1_metaprogram.cpp !common_linker_flags! || ( cl !common_compile_flags! !code_dir!\bt_secp256k1_metaprogram.cpp !common_linker_flags! || (
echo Build script failed because the metaprogram failed to build successfully echo Build script failed because the metaprogram failed to build successfully
exit exit
) )
@ -26,24 +26,3 @@ REM Code generate the single file header
echo Build script failed because the metaprogram did not run successfully echo Build script failed because the metaprogram did not run successfully
exit exit
) )
REM Build the test program to sanity check the single file header
pushd !build_dir!
set common_source_files=!code_dir!\bt_secp256k1_example.c
REM Build a C/C++ i64 version
cl !common_compile_flags! -Tc !common_source_files! !common_linker_flags! -out:bt_secp256k1_i64_example_c.exe
cl !common_compile_flags! -Tp !common_source_files! !common_linker_flags! -out:bt_secp256k1_i64_example_cpp.exe
where /q clang-cl || (
exit
)
REM (Optional) Build a C/C++ {i128, i128 x86_64 ASM} version
REM This requires clang-cl because MSVC does not expose a uint128_t type
clang-cl !common_compile_flags! -D EXAMPLE_BUILD_I128 -Tc !common_source_files! !common_linker_flags! -out:bt_secp256k1_i128_example_c.exe
clang-cl !common_compile_flags! -D EXAMPLE_BUILD_I128_X86_64_ASM -Tc !common_source_files! !common_linker_flags! -out:bt_secp256k1_i128_x86_64_asm_example_c.exe
clang-cl !common_compile_flags! -D EXAMPLE_BUILD_I128 -Tp !common_source_files! !common_linker_flags! -out:bt_secp256k1_i128_example_cpp.exe
clang-cl !common_compile_flags! -D EXAMPLE_BUILD_I128_X86_64_ASM -Tp !common_source_files! !common_linker_flags! -out:bt_secp256k1_i128_x86_64_asm_example_cpp.exe
popd

35
build_examples.bat Normal file
View File

@ -0,0 +1,35 @@
@echo off
setlocal EnableDelayedExpansion
where /q cl || (
echo MSVC's cl is not found - please run this from the MSVC x64 native tools command prompt
exit /b 1
)
set code_dir=%~dp0
set build_dir=!code_dir!\build
if not exist !build_dir! mkdir !build_dir!
set common_compile_flags=-W4 -Z7
set common_linker_flags=-link -nologo
REM Build the test program to sanity check the single file header
pushd !build_dir!
set common_source_files=!code_dir!\bt_secp256k1_example.c
REM Build a C/C++ i64 version
cl !common_compile_flags! -Tc !common_source_files! !common_linker_flags! -out:bt_secp256k1_i64_example_c.exe
cl !common_compile_flags! -Tp !common_source_files! !common_linker_flags! -out:bt_secp256k1_i64_example_cpp.exe
where /q clang-cl || (
exit
)
REM (Optional) Build a C/C++ {i128, i128 x86_64 ASM} version
REM This requires clang-cl because MSVC does not expose a uint128_t type
clang-cl !common_compile_flags! -D EXAMPLE_BUILD_I128 -Tc !common_source_files! !common_linker_flags! -out:bt_secp256k1_i128_example_c.exe
clang-cl !common_compile_flags! -D EXAMPLE_BUILD_I128_X86_64_ASM -Tc !common_source_files! !common_linker_flags! -out:bt_secp256k1_i128_x86_64_asm_example_c.exe
clang-cl !common_compile_flags! -D EXAMPLE_BUILD_I128 -Tp !common_source_files! !common_linker_flags! -out:bt_secp256k1_i128_example_cpp.exe
clang-cl !common_compile_flags! -D EXAMPLE_BUILD_I128_X86_64_ASM -Tp !common_source_files! !common_linker_flags! -out:bt_secp256k1_i128_x86_64_asm_example_cpp.exe
popd