Native Libraries:

- made C methods `static` (similar to `private` in Java) to avoid that they are added (exported) to shared library symbol table
- macOS and Linux: added `-fvisibility=hidden` to compiler options to mark C methods hidden by default
This commit is contained in:
Karl Tauber
2025-01-07 19:49:04 +01:00
parent d7462bd424
commit 251198c66d
10 changed files with 72 additions and 32 deletions

View File

@@ -14,6 +14,8 @@
* limitations under the License.
*/
import java.io.FileOutputStream
plugins {
`cpp-library`
`flatlaf-cpp-library`
@@ -67,7 +69,7 @@ tasks {
compilerArgs.addAll( toolChain.map {
when( it ) {
is Gcc, is Clang -> listOf()
is Gcc, is Clang -> listOf( "-fvisibility=hidden" )
else -> emptyList()
}
} )
@@ -108,6 +110,29 @@ tasks {
into( nativesDir )
rename( "libflatlaf-natives-linux.so", libraryName )
}
/*dump
val dylib = linkedFile.asFile.get()
val dylibDir = dylib.parent
exec { commandLine( "size", dylib ) }
exec {
commandLine( "objdump",
// commands
"--archive-headers",
"--section-headers",
"--private-headers",
"--reloc",
"--dynamic-reloc",
"--syms",
// options
// "--private-header",
// files
dylib )
standardOutput = FileOutputStream( "$dylibDir/objdump.txt" )
}
exec { commandLine( "objdump", "--disassemble-all", dylib ); standardOutput = FileOutputStream( "$dylibDir/disassemble.txt" ) }
exec { commandLine( "objdump", "--full-contents", dylib ); standardOutput = FileOutputStream( "$dylibDir/full-contents.txt" ) }
dump*/
}
}
}

View File

@@ -26,8 +26,8 @@
* @since 3.6
*/
// see X11WmUtils.cpp
Window getWindowHandle( JNIEnv* env, JAWT* awt, jobject window, Display** display_return );
// declare external methods
extern Window getWindowHandle( JNIEnv* env, JAWT* awt, jobject window, Display** display_return );
//---- class AutoReleaseStringUTF8 --------------------------------------------
@@ -55,12 +55,12 @@ public:
#define isOptionClear( option ) ((optionsClear & com_formdev_flatlaf_ui_FlatNativeLinuxLibrary_ ## option) != 0)
#define isOptionSetOrClear( option ) isOptionSet( option ) || isOptionClear( option )
jobjectArray newJavaStringArray( JNIEnv* env, jsize count ) {
static jobjectArray newJavaStringArray( JNIEnv* env, jsize count ) {
jclass stringClass = env->FindClass( "java/lang/String" );
return env->NewObjectArray( count, stringClass, NULL );
}
void initFilters( GtkFileChooser* chooser, JNIEnv* env, jint fileTypeIndex, jobjectArray fileTypes ) {
static void initFilters( GtkFileChooser* chooser, JNIEnv* env, jint fileTypeIndex, jobjectArray fileTypes ) {
jint length = env->GetArrayLength( fileTypes );
if( length <= 0 )
return;
@@ -89,7 +89,7 @@ void initFilters( GtkFileChooser* chooser, JNIEnv* env, jint fileTypeIndex, jobj
}
}
GdkWindow* getGdkWindow( JNIEnv* env, jobject window ) {
static GdkWindow* getGdkWindow( JNIEnv* env, jobject window ) {
// get the AWT
JAWT awt;
awt.version = JAWT_VERSION_1_4;

View File

@@ -25,11 +25,14 @@
*/
bool sendEvent( JNIEnv *env, jobject window, const char *atom_name,
long data0, long data1, long data2, long data3, long data4 );
bool isWMHintSupported( Display* display, Window rootWindow, Atom atom );
// declare exported methods
Window getWindowHandle( JNIEnv* env, JAWT* awt, jobject window, Display** display_return );
// declare internal methods
static bool sendEvent( JNIEnv *env, jobject window, const char *atom_name,
long data0, long data1, long data2, long data3, long data4 );
static bool isWMHintSupported( Display* display, Window rootWindow, Atom atom );
//---- JNI methods ------------------------------------------------------------
@@ -79,7 +82,7 @@ JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLinuxLibrary_xS
0 );
}
bool sendEvent( JNIEnv *env, jobject window, const char *atom_name,
static bool sendEvent( JNIEnv *env, jobject window, const char *atom_name,
long data0, long data1, long data2, long data3, long data4 )
{
// get the AWT
@@ -131,7 +134,7 @@ bool sendEvent( JNIEnv *env, jobject window, const char *atom_name,
}
bool isWMHintSupported( Display* display, Window rootWindow, Atom atom ) {
static bool isWMHintSupported( Display* display, Window rootWindow, Atom atom ) {
Atom type;
int format;
unsigned long n_atoms;

View File

@@ -72,7 +72,7 @@ tasks {
compilerArgs.addAll( toolChain.map {
when( it ) {
is Gcc, is Clang -> listOf( "-x", "objective-c++", "-mmacosx-version-min=$minOs" )
is Gcc, is Clang -> listOf( "-x", "objective-c++", "-mmacosx-version-min=$minOs", "-fvisibility=hidden" )
else -> emptyList()
}
} )

View File

@@ -126,10 +126,10 @@
#define isOptionClear( option ) ((optionsClear & com_formdev_flatlaf_ui_FlatNativeMacLibrary_ ## option) != 0)
#define isOptionSetOrClear( option ) isOptionSet( option ) || isOptionClear( option )
// declare methods
NSWindow* getNSWindow( JNIEnv* env, jclass cls, jobject window );
// declare external methods
extern NSWindow* getNSWindow( JNIEnv* env, jclass cls, jobject window );
jobjectArray newJavaStringArray( JNIEnv* env, jsize count ) {
static jobjectArray newJavaStringArray( JNIEnv* env, jsize count ) {
jclass stringClass = env->FindClass( "java/lang/String" );
return env->NewObjectArray( count, stringClass, NULL );
}

View File

@@ -39,13 +39,15 @@
@implementation WindowData
@end
// declare internal methods
// declare exported methods
NSWindow* getNSWindow( JNIEnv* env, jclass cls, jobject window );
WindowData* getWindowData( NSWindow* nsWindow, bool allocate );
void setWindowButtonsHidden( NSWindow* nsWindow, bool hidden );
int getWindowButtonAreaWidth( NSWindow* nsWindow );
int getWindowTitleBarHeight( NSWindow* nsWindow );
bool isWindowFullScreen( NSWindow* nsWindow );
// declare internal methods
static WindowData* getWindowData( NSWindow* nsWindow, bool allocate );
static void setWindowButtonsHidden( NSWindow* nsWindow, bool hidden );
static int getWindowButtonAreaWidth( NSWindow* nsWindow );
static int getWindowTitleBarHeight( NSWindow* nsWindow );
static bool isWindowFullScreen( NSWindow* nsWindow );
NSWindow* getNSWindow( JNIEnv* env, jclass cls, jobject window ) {
@@ -79,7 +81,7 @@ NSWindow* getNSWindow( JNIEnv* env, jclass cls, jobject window ) {
return (NSWindow *) jlong_to_ptr( env->GetLongField( platformWindow, ptrID ) );
}
WindowData* getWindowData( NSWindow* nsWindow, bool allocate ) {
static WindowData* getWindowData( NSWindow* nsWindow, bool allocate ) {
static char key;
WindowData* windowData = objc_getAssociatedObject( nsWindow, &key );
if( windowData == NULL && allocate ) {
@@ -243,7 +245,7 @@ JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setW
return FALSE;
}
void setWindowButtonsHidden( NSWindow* nsWindow, bool hidden ) {
static void setWindowButtonsHidden( NSWindow* nsWindow, bool hidden ) {
// get buttons
NSView* buttons[3] = {
[nsWindow standardWindowButton:NSWindowCloseButton],
@@ -303,7 +305,7 @@ JNIEXPORT jobject JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWi
return NULL;
}
int getWindowButtonAreaWidth( NSWindow* nsWindow ) {
static int getWindowButtonAreaWidth( NSWindow* nsWindow ) {
// get buttons
NSView* buttons[3] = {
[nsWindow standardWindowButton:NSWindowCloseButton],
@@ -335,7 +337,7 @@ int getWindowButtonAreaWidth( NSWindow* nsWindow ) {
return right + left;
}
int getWindowTitleBarHeight( NSWindow* nsWindow ) {
static int getWindowTitleBarHeight( NSWindow* nsWindow ) {
NSView* closeButton = [nsWindow standardWindowButton:NSWindowCloseButton];
if( closeButton == NULL )
return -1;
@@ -360,7 +362,7 @@ JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_isWi
return FALSE;
}
bool isWindowFullScreen( NSWindow* nsWindow ) {
static bool isWindowFullScreen( NSWindow* nsWindow ) {
return ((nsWindow.styleMask & NSWindowStyleMaskFullScreen) != 0);
}

View File

@@ -93,6 +93,15 @@ tasks {
into( nativesDir )
rename( "flatlaf-natives-windows.dll", libraryName )
}
/*dump
val dumpbin = "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.42.34433/bin/Hostx64/x64/dumpbin.exe"
val dll = linkedFile.asFile.get()
val dllDir = dll.parent
exec { commandLine( dumpbin, "/all", "/rawdata:none", "/out:$dllDir/objdump.txt", dll ) }
exec { commandLine( dumpbin, "/all", "/out:$dllDir/full-contents.txt", dll ) }
exec { commandLine( dumpbin, "/disasm", "/out:$dllDir/disassemble.txt", dll ) }
dump*/
}
}
}

View File

@@ -30,6 +30,7 @@
* @author Karl Tauber
*/
// declare exported methods
HWND getWindowHandle( JNIEnv* env, jobject window );
//---- JNI methods ------------------------------------------------------------

View File

@@ -26,8 +26,8 @@
* @since 3.6
*/
// see FlatWndProc.cpp
HWND getWindowHandle( JNIEnv* env, jobject window );
// declare external methods
extern HWND getWindowHandle( JNIEnv* env, jobject window );
//---- class AutoReleasePtr ---------------------------------------------------
@@ -147,12 +147,12 @@ public:
#define isOptionSet( option ) ((optionsSet & com_formdev_flatlaf_ui_FlatNativeWindowsLibrary_ ## option) != 0)
#define CHECK_HRESULT( code ) { if( (code) != S_OK ) return NULL; }
jobjectArray newJavaStringArray( JNIEnv* env, jsize count ) {
static jobjectArray newJavaStringArray( JNIEnv* env, jsize count ) {
jclass stringClass = env->FindClass( "java/lang/String" );
return env->NewObjectArray( count, stringClass, NULL );
}
jstring newJavaString( JNIEnv* env, LPWSTR str ) {
static jstring newJavaString( JNIEnv* env, LPWSTR str ) {
return env->NewString( reinterpret_cast<jchar*>( str ), static_cast<jsize>( wcslen( str ) ) );
}

View File

@@ -25,8 +25,8 @@
* @author Karl Tauber
*/
// see FlatWndProc.cpp
HWND getWindowHandle( JNIEnv* env, jobject window );
// declare external methods
extern HWND getWindowHandle( JNIEnv* env, jobject window );
//---- Utility ----------------------------------------------------------------