Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/how/cmake_bgfx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ if(TSLANG_MEMORY_MODEL STREQUAL "gc")
list(APPEND TSLANG_LINK_LIBS gc)
endif()

# legacy_stdio_definitions: printf, sprintf_s and the rest of stdio as functions compiled code can
# call (the UCRT has them only as inline wrappers in its headers)
if(WIN32)
list(APPEND TSLANG_LINK_LIBS ntdll legacy_stdio_definitions)
list(APPEND TSLANG_LINK_LIBS ntdll)
else()
find_library(TSLANG_TINFO_LIB NAMES tinfo)
list(APPEND TSLANG_LINK_LIBS LLVMDemangle stdc++ m pthread dl rt)
Expand Down
6 changes: 2 additions & 4 deletions docs/how/cmake_dll/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc")
list(APPEND TSLANG_LINK_LIBS "gcdll/gc")
endif()

# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows; legacy_stdio_definitions
# printf, sprintf_s and the rest of stdio as functions compiled code can call (the UCRT has them only
# as inline wrappers in its headers)
# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows
if(WIN32)
list(APPEND TSLANG_LINK_LIBS "ntdll" "legacy_stdio_definitions")
list(APPEND TSLANG_LINK_LIBS "ntdll")
else()
list(APPEND TSLANG_LINK_LIBS "LLVMDemangle" "stdc++" "m" "pthread" "tinfo" "dl" "rt")
endif()
Expand Down
6 changes: 2 additions & 4 deletions docs/how/cmake_tslang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc")
endif()
endif()

# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows; legacy_stdio_definitions
# printf, sprintf_s and the rest of stdio as functions compiled code can call (the UCRT has them only
# as inline wrappers in its headers)
# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows
if(WIN32)
list(APPEND TSLANG_LINK_LIBS "ntdll" "legacy_stdio_definitions")
list(APPEND TSLANG_LINK_LIBS "ntdll")
else()
list(APPEND TSLANG_LINK_LIBS "LLVMDemangle" "stdc++" "m" "pthread" "tinfo" "dl" "rt")
endif()
Expand Down
4 changes: 1 addition & 3 deletions docs/how/cmake_winapp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ target_link_directories(
${GCLIBPATH}
)

# legacy_stdio_definitions: printf, sprintf_s and the rest of stdio as functions compiled code can
# call (the UCRT has them only as inline wrappers in its headers)
set (LIBS "gcmt-lib" "legacy_stdio_definitions")
set (LIBS "gcmt-lib")

target_link_libraries(
${PROJECT_NAME}
Expand Down
6 changes: 1 addition & 5 deletions tslang/include/TypeScript/VSCodeTemplate/Files.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,7 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc")
list(APPEND TSLANG_LINK_LIBS "gc")
endif()

# printf, sprintf_s and the rest of stdio as functions compiled code can call: the UCRT has them only
# as inline wrappers in its headers, which code tslang compiles never sees.
if(WIN32)
list(APPEND TSLANG_LINK_LIBS "legacy_stdio_definitions")
else()
if(NOT WIN32)
list(APPEND TSLANG_LINK_LIBS "stdc++" "m" "pthread" "dl" "rt")
endif()

Expand Down
1 change: 1 addition & 0 deletions tslang/lib/TypeScriptAsyncRuntime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_mlir_library(TypeScriptAsyncRuntime
ProcessHeap.cpp
DynamicRuntime.cpp
UncaughtException.cpp
StdioDefinitions.cpp

EXCLUDE_FROM_LIBMLIR

Expand Down
92 changes: 92 additions & 0 deletions tslang/lib/TypeScriptAsyncRuntime/StdioDefinitions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// printf, sprintf_s, scanf and the rest of stdio, as functions a compiled program can call.
//
// The UCRT has them only as inline functions in its headers, over __stdio_common_vfprintf and the
// like: its libraries define none of them (x64's libucrt has printf alone, x86's nothing). A C or C++
// caller gets its own copy from the header; code tslang compiles never sees the header, so a
// `@varargs declare function printf` - or the sprintf_s a number's conversion to text calls - linked
// only when something else happened to bring a copy in. Microsoft's answer is
// legacy_stdio_definitions.lib, one more library in every link line.
//
// Instead, this file takes the address of each one, which makes the compiler emit the header's own
// definitions here, as linkable COMDATs (`selectany`), and the runtime every program links already
// carries them. The same kind of COMDAT a C or C++ object holds when it calls one: two copies are one,
// so a program whose other objects have a copy of their own links as it did. The file is its own
// member of the archive: the linker takes it only for a function nothing else defined.
#if defined(_WIN32)

#include <stdarg.h>
#include <stdio.h>
#include <wchar.h>

#define TSLANG_STDIO(name, type) reinterpret_cast<const void *>(static_cast<type>(&name))

extern "C" const void *const __tslang_stdio_definitions[] = {
// printf
TSLANG_STDIO(printf, int (*)(const char *, ...)),
TSLANG_STDIO(fprintf, int (*)(FILE *, const char *, ...)),
TSLANG_STDIO(sprintf, int (*)(char *, const char *, ...)),
TSLANG_STDIO(snprintf, int (*)(char *, size_t, const char *, ...)),
TSLANG_STDIO(_snprintf, int (*)(char *, size_t, const char *, ...)),
TSLANG_STDIO(_scprintf, int (*)(const char *, ...)),
TSLANG_STDIO(vprintf, int (*)(const char *, va_list)),
TSLANG_STDIO(vfprintf, int (*)(FILE *, const char *, va_list)),
TSLANG_STDIO(vsprintf, int (*)(char *, const char *, va_list)),
TSLANG_STDIO(vsnprintf, int (*)(char *, size_t, const char *, va_list)),
TSLANG_STDIO(_vsnprintf, int (*)(char *, size_t, const char *, va_list)),
TSLANG_STDIO(_vscprintf, int (*)(const char *, va_list)),
TSLANG_STDIO(printf_s, int (*)(const char *, ...)),
TSLANG_STDIO(fprintf_s, int (*)(FILE *, const char *, ...)),
TSLANG_STDIO(sprintf_s, int (*)(char *, size_t, const char *, ...)),
TSLANG_STDIO(_snprintf_s, int (*)(char *, size_t, size_t, const char *, ...)),
TSLANG_STDIO(vprintf_s, int (*)(const char *, va_list)),
TSLANG_STDIO(vfprintf_s, int (*)(FILE *, const char *, va_list)),
TSLANG_STDIO(vsprintf_s, int (*)(char *, size_t, const char *, va_list)),
TSLANG_STDIO(vsnprintf_s, int (*)(char *, size_t, size_t, const char *, va_list)),
TSLANG_STDIO(_vsnprintf_s, int (*)(char *, size_t, size_t, const char *, va_list)),

// scanf
TSLANG_STDIO(scanf, int (*)(const char *, ...)),
TSLANG_STDIO(fscanf, int (*)(FILE *, const char *, ...)),
TSLANG_STDIO(sscanf, int (*)(const char *, const char *, ...)),
TSLANG_STDIO(vscanf, int (*)(const char *, va_list)),
TSLANG_STDIO(vfscanf, int (*)(FILE *, const char *, va_list)),
TSLANG_STDIO(vsscanf, int (*)(const char *, const char *, va_list)),
TSLANG_STDIO(scanf_s, int (*)(const char *, ...)),
TSLANG_STDIO(fscanf_s, int (*)(FILE *, const char *, ...)),
TSLANG_STDIO(sscanf_s, int (*)(const char *, const char *, ...)),
TSLANG_STDIO(vscanf_s, int (*)(const char *, va_list)),
TSLANG_STDIO(vfscanf_s, int (*)(FILE *, const char *, va_list)),
TSLANG_STDIO(vsscanf_s, int (*)(const char *, const char *, va_list)),

// wprintf
TSLANG_STDIO(wprintf, int (*)(const wchar_t *, ...)),
TSLANG_STDIO(fwprintf, int (*)(FILE *, const wchar_t *, ...)),
TSLANG_STDIO(swprintf, int (*)(wchar_t *, size_t, const wchar_t *, ...)),
TSLANG_STDIO(_snwprintf, int (*)(wchar_t *, size_t, const wchar_t *, ...)),
TSLANG_STDIO(vwprintf, int (*)(const wchar_t *, va_list)),
TSLANG_STDIO(vfwprintf, int (*)(FILE *, const wchar_t *, va_list)),
TSLANG_STDIO(vswprintf, int (*)(wchar_t *, size_t, const wchar_t *, va_list)),
TSLANG_STDIO(_vsnwprintf, int (*)(wchar_t *, size_t, const wchar_t *, va_list)),
TSLANG_STDIO(wprintf_s, int (*)(const wchar_t *, ...)),
TSLANG_STDIO(fwprintf_s, int (*)(FILE *, const wchar_t *, ...)),
TSLANG_STDIO(swprintf_s, int (*)(wchar_t *, size_t, const wchar_t *, ...)),
TSLANG_STDIO(vwprintf_s, int (*)(const wchar_t *, va_list)),
TSLANG_STDIO(vfwprintf_s, int (*)(FILE *, const wchar_t *, va_list)),
TSLANG_STDIO(vswprintf_s, int (*)(wchar_t *, size_t, const wchar_t *, va_list)),

// wscanf
TSLANG_STDIO(wscanf, int (*)(const wchar_t *, ...)),
TSLANG_STDIO(fwscanf, int (*)(FILE *, const wchar_t *, ...)),
TSLANG_STDIO(swscanf, int (*)(const wchar_t *, const wchar_t *, ...)),
TSLANG_STDIO(vwscanf, int (*)(const wchar_t *, va_list)),
TSLANG_STDIO(vfwscanf, int (*)(FILE *, const wchar_t *, va_list)),
TSLANG_STDIO(vswscanf, int (*)(const wchar_t *, const wchar_t *, va_list)),
TSLANG_STDIO(wscanf_s, int (*)(const wchar_t *, ...)),
TSLANG_STDIO(fwscanf_s, int (*)(FILE *, const wchar_t *, ...)),
TSLANG_STDIO(swscanf_s, int (*)(const wchar_t *, const wchar_t *, ...)),
TSLANG_STDIO(vwscanf_s, int (*)(const wchar_t *, va_list)),
TSLANG_STDIO(vfwscanf_s, int (*)(FILE *, const wchar_t *, va_list)),
TSLANG_STDIO(vswscanf_s, int (*)(const wchar_t *, const wchar_t *, va_list)),
};

#endif
4 changes: 3 additions & 1 deletion tslang/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ set(SRC "${CMAKE_CURRENT_SOURCE_DIR}/../lib/TypeScriptAsyncRuntime")
# program using async does not link (GC_enable_threads); ProcessHeap.cpp is empty off Windows. Without
# UncaughtException.cpp an x86 program that threw what nothing caught ended in abort()'s message box,
# which an unattended run waits on forever (exe.cpp takes the file only when the runtime defines it).
# Without StdioDefinitions.cpp a program calling printf or sprintf_s does not link on Windows.
add_library(TypeScriptAsyncRuntime STATIC
"${SRC}/AsyncRuntime.cpp"
"${SRC}/AsyncGCThreads.cpp"
"${SRC}/ProcessHeap.cpp"
"${SRC}/DynamicRuntime.cpp"
"${SRC}/UncaughtException.cpp")
"${SRC}/UncaughtException.cpp"
"${SRC}/StdioDefinitions.cpp")
target_include_directories(TypeScriptAsyncRuntime PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../include"
"${CMAKE_CURRENT_SOURCE_DIR}/../lib"
Expand Down
6 changes: 3 additions & 3 deletions tslang/test/tester/test-runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#define TYPESCRIPT_LIB "TypeScriptAsyncRuntime.lib "
//#define LIBS "msvcrt" _D_ ".lib ucrt" _D_ ".lib "
// static CRT (/MT[d]) to match LLVM/TypeScript runtime libs and gc.lib; mixing static+dynamic CRT crashes at startup
// legacy_stdio_definitions: printf and the rest of stdio as functions a program can call, which the
// UCRT has only as inline wrappers - x86's libucrt defines none of them (x64's happens to have printf)
#define LIBS "libcmt" _D_ ".lib libvcruntime" _D_ ".lib libucrt" _D_ ".lib legacy_stdio_definitions.lib "
// printf and the rest of stdio, which the UCRT has only as inline wrappers, come from TYPESCRIPT_LIB
// (TypeScriptAsyncRuntime/StdioDefinitions.cpp)
#define LIBS "libcmt" _D_ ".lib libvcruntime" _D_ ".lib libucrt" _D_ ".lib "
#else
// for Ubuntu 20.04 add -ldl and optionally -rdynamic
#define LIBS "-frtti -fexceptions -lstdc++ -lrt -ldl -lpthread -lm"
Expand Down
6 changes: 0 additions & 6 deletions tslang/tslang/exe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1095,12 +1095,6 @@ int linkBinary(const char *argv0, const LinkRequest &request, CompileOptions &co
args.push_back("-Wl,-nodefaultlib:libcmt");
}

// printf and the rest of stdio as functions a program can call (a `@varargs declare function
// printf`): the UCRT has them only as inline wrappers in its headers. x86's libucrt defines none, and
// a 32-bit program calling printf failed to link (undefined symbol _printf); x64's libucrt happens
// to define printf, which it still takes, being ahead of this.
args.push_back("-llegacy_stdio_definitions");

// Windows asks for elevation before starting a 32-bit program that has no
// requestedExecutionLevel and whose name contains "setup", "install", "update" or "patch"
// (installer detection), so `my_setup.exe` or `dispatch.exe` would never start. Link an
Expand Down
Loading