Skip to content

Commit

Permalink
Added optimizationLevel to port
Browse files Browse the repository at this point in the history
  • Loading branch information
ypujante committed Jul 31, 2024
1 parent cfe4397 commit daaf33d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5)

set(emscripten-glfw_RELEASE_YEAR "2024")
set(emscripten-glfw_RELEASE_MONTH "07" )
set(emscripten-glfw_RELEASE_DAY "27" )
set(emscripten-glfw_RELEASE_DAY "31" )

set(emscripten-glfw_GLFW_VERSION "3.4.0")

Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,22 @@ LDFLAGS += -s USE_WEBGPU=1 --js-library $(EMS_GLFW3_DIR)/src/js/lib_emscripten_g
Release Notes
-------------
#### 3.4.0.20240731 - 2024-07-31 | emscripten TBD
- Added `emscripten_glfw_get_clipboard_string` the C version of `emscripten::glfw3::GetClipboardString` to
retrieve the clipboard asynchronously
- Added a helper class `emscripten::glfw3::FutureClipboardString` to greatly simplify the more frequent use-cases
- `GetClipboardString::value()` now returns the internal clipboard in case of error, instead of throwing exception
- Added `optimizationLevel` option to the emscripten port
#### 3.4.0.20240727 - 2024-07-27 | emscripten TBD
- Introduced C++ API (namespace `emscripten::glfw3`) included with `GLFW3/emscripten_glfw3.h`:
- provides a more correct API with sensible defaults (ex: `std::string_view` / `std::optional<std::string_view>`
vs `char const *` which may or may not be `nullptr`)
- allow for C++ only API (ex: `std::future`)
- the C API is still available if you would rather stick to it
- Implemented `emscripten::glfw3::GetClipboardString` (C++ only) which provides a way of fetching the global
- Implemented `emscripten::glfw3::GetClipboardString` which provides a way of fetching the global
clipboard in a browser environment (`glfwGetClipboardString` is not the right API due to the asynchronous nature
of the underlying platform API).
- The cursor position is no longer clamped to the window size, and as a result, can have negative values or values
Expand Down
53 changes: 35 additions & 18 deletions port/emscripten-glfw3.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# @author Yan Pujante

import os
from typing import Dict
from typing import Union, Dict

TAG = '3.4.0.20240727'
HASH = 'aa90c76e0d87166c1db13d5219a47af6bd2e690d30e3528d4d8993b45e5f699c4f94ad3f0ca77be096807d68efe3189ffd21985dae072b46c9039060e186a58c'
Expand All @@ -26,22 +26,33 @@
DESCRIPTION = 'This project is an emscripten port of GLFW 3.4 written in C++ for the web/webassembly platform'
LICENSE = 'Apache 2.0 license'

VALID_OPTION_VALUES = {
'disableWarning': ['true', 'false'],
'disableJoystick': ['true', 'false'],
'disableMultiWindow': ['true', 'false'],
'optimizationLevel': ['0', '1', '2', '3', 'g', 's', 'z'] # all -OX possibilities
}

OPTIONS = {
'disableWarning': 'Boolean to disable warnings emitted by the library',
'disableJoystick': 'Boolean to disable support for joystick entirely',
'disableMultiWindow': 'Boolean to disable multi window support which makes the code smaller and faster'
'disableMultiWindow': 'Boolean to disable multi window support which makes the code smaller and faster',
'optimizationLevel': f'Optimization level: {VALID_OPTION_VALUES["optimizationLevel"]} (default to 2)',
}

# user options (from --use-port)
opts: Dict[str, bool] = {
opts: Dict[str, Union[str, bool]] = {
'disableWarning': False,
'disableJoystick': False,
'disableMultiWindow': False
'disableMultiWindow': False,
'optimizationLevel': '2'
}

port_name = 'emscripten-glfw3'


def get_lib_name(settings):
return (f'lib_{name}_{TAG}' +
return (f'lib_{port_name}_{TAG}-O{opts["optimizationLevel"]}' +
('-nw' if opts['disableWarning'] else '') +
('-nj' if opts['disableJoystick'] else '') +
('-sw' if opts['disableMultiWindow'] else '') +
Expand All @@ -50,17 +61,17 @@ def get_lib_name(settings):

def get(ports, settings, shared):
# get the port
ports.fetch_project(name, ZIP_URL, sha512hash=HASH)
ports.fetch_project(port_name, ZIP_URL, sha512hash=HASH)

def create(final):
root_path = os.path.join(ports.get_dir(), name)
root_path = os.path.join(ports.get_dir(), port_name)
source_path = os.path.join(root_path, 'src', 'cpp')
source_include_paths = [os.path.join(root_path, 'external'), os.path.join(root_path, 'include')]
target = os.path.join(name, 'GLFW')
target = os.path.join(port_name, 'GLFW')
for source_include_path in source_include_paths:
ports.install_headers(os.path.join(source_include_path, 'GLFW'), target=target)

flags = []
flags = [f'-O{opts["optimizationLevel"]}']

if opts['disableWarning']:
flags += ['-DEMSCRIPTEN_GLFW3_DISABLE_WARNING']
Expand All @@ -71,20 +82,21 @@ def create(final):
if opts['disableMultiWindow']:
flags += ['-DEMSCRIPTEN_GLFW3_DISABLE_MULTI_WINDOW_SUPPORT']

ports.build_port(source_path, final, name, includes=source_include_paths, flags=flags)
ports.build_port(source_path, final, port_name, includes=source_include_paths, flags=flags)

lib = shared.cache.get_lib(get_lib_name(settings), create, what='port')
if os.path.getmtime(lib) < os.path.getmtime(__file__):
clear(ports, settings, shared)
lib = shared.cache.get_lib(get_lib_name(settings), create, what='port')
clear(ports, settings, shared)
lib = shared.cache.get_lib(get_lib_name(settings), create, what='port')
return [lib]


def clear(ports, settings, shared):
shared.cache.erase_lib(get_lib_name(settings))


def linker_setup(ports, settings):
root_path = os.path.join(ports.get_dir(), name)
root_path = os.path.join(ports.get_dir(), port_name)
source_js_path = os.path.join(root_path, 'src', 'js', 'lib_emscripten_glfw3.js')
settings.JS_LIBRARIES += [source_js_path]

Expand All @@ -93,12 +105,17 @@ def linker_setup(ports, settings):
# so that we don't conflict with the builtin GLFW headers that emscripten
# includes
def process_args(ports):
return ['-isystem', ports.get_include_dir(name), '-DEMSCRIPTEN_USE_PORT_CONTRIB_GLFW3']
return ['-isystem', ports.get_include_dir(port_name), '-DEMSCRIPTEN_USE_PORT_CONTRIB_GLFW3']


def check_option(option, value, error_handler):
if value not in VALID_OPTION_VALUES[option]:
error_handler(f'[{option}] can be {list(VALID_OPTION_VALUES[option])}, got [{value}]')
if isinstance(opts[option], bool):
value = value == 'true'
return value


def handle_options(options, error_handler):
for option, value in options.items():
if value.lower() in {'true', 'false'}:
opts[option] = value.lower() == 'true'
else:
error_handler(f'{option} is expecting a boolean, got {value}')
opts[option] = check_option(option, value.lower(), error_handler)
2 changes: 1 addition & 1 deletion src/cpp/emscripten/glfw3/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace emscripten::glfw3 {

#define D_EMSCRIPTEN_GLFW_VERSION_STR "3.4.0.20240727"
#define D_EMSCRIPTEN_GLFW_VERSION_STR "3.4.0.20240731"

}

Expand Down

0 comments on commit daaf33d

Please sign in to comment.