Skip to content

Commit

Permalink
change the architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadRaziei committed Oct 18, 2024
1 parent fe3ac4f commit cea16e2
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 71 deletions.
172 changes: 109 additions & 63 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
cmake_minimum_required(VERSION 3.12)

option(BUILD_PYTHON "export python module" off)

set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ version selection")
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CXX_EXTENSIONS OFF)


list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
include(PyProject)
message(STATUS "Project: ${PyProject_NAME}@v${PyProject_VERSION}")


if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BUILD_TYPE Release)
endif()

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")

#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")


#if(MSVC)
# # Microsoft Visual Studio compiler
# set(MSVC_FLAGS "/O2")
# set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${MSVC_FLAGS}")
#elseif(CMAKE_COMPILER_IS_GNUCXX)
# # GCC or Clang compiler
# set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
# if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# # Linux
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
# else()
# # Other OS
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=generic")
# endif()
#endif()

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "" FORCE)

project(${PyProject_NAME} LANGUAGES CXX)
project(${PyProject_NAME} LANGUAGES CXX VERSION ${PyProject_VERSION})
message(STATUS "Project: ${PROJECT_NAME}@v${PROJECT_VERSION}")

find_package(Python3 REQUIRED COMPONENTS Interpreter)

if(DEFINED SKBUILD)
set(PYTHON_PROJECT_NAME "${SKBUILD_PROJECT_NAME}")
elseif(BUILD_PYTHON)
set(PYTHON_PROJECT_NAME "${CMAKE_BINARY_DIR}")

if(NOT PYTHON_REQUIREMENT_INSTALLED)
execute_process(
COMMAND "${Python3_EXECUTABLE}" -m pip install
nanobind ninja pytest-xdist # build requirements
OUTPUT_QUIET
)
set(PYTHON_REQUIREMENT_INSTALLED TRUE CACHE INTERNAL "Python requirements installed")
endif()

execute_process(
COMMAND "${Python3_EXECUTABLE}" -m nanobind --cmake_dir
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE NB_DIR)

message(STATUS "Found NanoBind at ${NB_DIR}")
list(APPEND CMAKE_PREFIX_PATH "${NB_DIR}")
endif()



# Download the public suffix list file from the internet
Expand All @@ -66,57 +73,88 @@ endif()
file(GLOB SOURCES ${PROJECT_SOURCE_DIR}/src/*.cpp)


if(DEFINED SKBUILD)
if(DEFINED PYTHON_PROJECT_NAME)
# Try to import all Python components potentially needed by nanobind
#set(Python3_FIND_STRATEGY LOCATION)
find_package(Python 3.8
REQUIRED COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)

REQUIRED COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)
# Import nanobind through CMake's find_package mechanism
find_package(nanobind CONFIG REQUIRED)

# We are now ready to compile the actual extension module
nanobind_add_module(
# Name of the extension
_core

# Target the stable ABI for Python 3.12+, which reduces
# the number of binary wheels that must be built. This
# does nothing on older Python versions
STABLE_ABI

# Build libnanobind statically and merge it into the
# extension (which itself remains a shared library)
#
# If your project builds multiple extensions, you can
# replace this flag by NB_SHARED to conserve space by
# reusing a shared libnanobind across libraries
NB_STATIC

# Source code goes here
src/binding/main.cpp
)

target_sources(_core PRIVATE ${SOURCES})
target_include_directories(_core PRIVATE include src)
target_compile_definitions(_core PRIVATE VERSION_INFO=${SKBUILD_PROJECT_VERSION})
target_compile_definitions(_core PRIVATE
DONT_INIT_PSL
PUBLIC_SUFFIX_LIST_URL="${PUBLIC_SUFFIX_LIST_URL}"
PUBLIC_SUFFIX_LIST_DAT="${PUBLIC_SUFFIX_LIST_DAT}"
)
set(NB_MODULE _about)
nanobind_add_module(${NB_MODULE} STABLE_ABI NB_STATIC ${PROJECT_SOURCE_DIR}/src/binding/about.cpp)
target_compile_definitions(${NB_MODULE} PRIVATE
VERSION_INFO=${PROJECT_VERSION}
NB_MODULE_NAME=${NB_MODULE})

file(GLOB PYTHON_BIND_MODULES_PATH ${PROJECT_SOURCE_DIR}/src/binding/bind_*)
set(PYTHON_BIND_MODULES "${NB_MODULE}")
foreach(NB_MODULE_PATH IN LISTS PYTHON_BIND_MODULES_PATH)
get_filename_component(NB_MODULE ${NB_MODULE_PATH} NAME)
string(REGEX REPLACE "bind(.*)" "\\1" NB_MODULE ${NB_MODULE})
list(APPEND PYTHON_BIND_MODULES ${NB_MODULE})
file(GLOB NB_MODULE_SOURCES ${NB_MODULE_PATH}/*.cpp)

message(STATUS "Found module: ${NB_MODULE}")

# Add more commands here to process each directory as needed

# We are now ready to compile the actual extension module
nanobind_add_module(
# Name of the extension
${NB_MODULE}

# Target the stable ABI for Python 3.12+, which reduces
# the number of binary wheels that must be built. This
# does nothing on older Python versions
STABLE_ABI

# Build libnanobind statically and merge it into the
# extension (which itself remains a shared library)
#
# If your project builds multiple extensions, you can
# replace this flag by NB_SHARED to conserve space by
# reusing a shared libnanobind across libraries
NB_STATIC

# Source code goes here
${NB_MODULE_SOURCES} ${SOURCES}
)

target_include_directories(${NB_MODULE} PRIVATE include src)
target_compile_definitions(${NB_MODULE} PRIVATE NB_MODULE_NAME=${NB_MODULE})
target_compile_definitions(${NB_MODULE} PRIVATE
DONT_INIT_PSL
PUBLIC_SUFFIX_LIST_URL="${PUBLIC_SUFFIX_LIST_URL}"
PUBLIC_SUFFIX_LIST_DAT="${PUBLIC_SUFFIX_LIST_DAT}"
)
endforeach()

if(DEFINED SKBUILD)
install(TARGETS ${PYTHON_BIND_MODULES}
CONFIGURATIONS Release
LIBRARY DESTINATION
${PYTHON_PROJECT_NAME})
install (FILES ${PUBLIC_SUFFIX_LIST_DAT}
DESTINATION ${SKBUILD_PROJECT_NAME})
else()
file(COPY ${PROJECT_SOURCE_DIR}/src/${PROJECT_NAME} DESTINATION ${PYTHON_PROJECT_NAME})
file(COPY ${PUBLIC_SUFFIX_LIST_DAT} DESTINATION ${PYTHON_PROJECT_NAME}/${PROJECT_NAME} )

install(TARGETS _core
CONFIGURATIONS Release
LIBRARY DESTINATION
${SKBUILD_PROJECT_NAME})
install (FILES ${PUBLIC_SUFFIX_LIST_DAT}
DESTINATION ${SKBUILD_PROJECT_NAME})
foreach(_core IN LISTS PYTHON_BIND_MODULES)
set_target_properties(${_core} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PYTHON_PROJECT_NAME}/${PROJECT_NAME})
endforeach()
endif()
endif()

if(DEFINED SKBUILD)
RETURN()
endif()



add_library(urlparser SHARED ${SOURCES})
add_library(url::base ALIAS urlparser)
target_compile_definitions(urlparser PRIVATE
Expand Down Expand Up @@ -152,6 +190,14 @@ add_test(NAME unitTests COMMAND test_liburlparser)
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} -V DEPENDS test_liburlparser)


if(BUILD_PYTHON)
add_test(NAME pythonTests
COMMAND ${Python3_EXECUTABLE} -m pytest -n auto ${PROJECT_SOURCE_DIR}/tests/python
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
endif()



# Find Doxygen package
find_package(Doxygen)
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions src/binding/about.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by mohammad on 10/17/24.
//

#include <nanobind/nanobind.h>

#include <string>

#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)


NB_MODULE(NB_MODULE_NAME, m) {
#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif
}
8 changes: 2 additions & 6 deletions src/binding/main.cpp → src/binding/bind_core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ inline nb::dict extract(const std::string& host){
return host_to_dict_minimal(TLD::Host(host));
}

NB_MODULE(_core, m) {
NB_MODULE(NB_MODULE_NAME, m) {
m.doc() = R"pbdoc(
nanobind example plugin
-----------------------
Expand Down Expand Up @@ -150,9 +150,5 @@ NB_MODULE(_core, m) {
return std::string("<PSL : ") + (p.isLoaded() ? "loaded" : "not loaded") + ">";
});

#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif

}
3 changes: 2 additions & 1 deletion src/liburlparser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from .core import Host, Url, __doc__, __version__, psl
from ._about import __version__
from .core import Host, Url, __doc__, psl

__all__ = [
"__doc__", "__version__", "Url", "Host", "psl"
Expand Down
2 changes: 1 addition & 1 deletion src/liburlparser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from filelock import FileLock

from ._core import Host, Psl, Url, __doc__, __version__
from ._core import Host, Psl, Url, __doc__

psl = Psl() # psl

Expand Down

0 comments on commit cea16e2

Please sign in to comment.