Skip to content

Commit

Permalink
use modern cmake python3 detection, re-enable macOS ci, provide more …
Browse files Browse the repository at this point in the history
…specific error message
  • Loading branch information
d99kris committed Feb 25, 2023
1 parent 03e2ac8 commit 2b7c497
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 17 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: macOS

on:
pull_request:
push:
branches:
- master
- main
- dev/*

jobs:
mac-build:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Build macOS
run: ./make.sh all
21 changes: 12 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# spacy-cpp is distributed under the MIT license, see LICENSE for details.

# Project
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(spacy-cpp VERSION 1.07 LANGUAGES CXX)
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(spacy-cpp VERSION 1.08 LANGUAGES CXX)
set (CMAKE_CXX_STANDARD 11)
if(MSVC)
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
Expand All @@ -22,8 +22,8 @@ else()
-Wcast-qual -Wno-missing-braces -Wswitch-default -Wunreachable-code \
-Wundef -Wuninitialized -Wcast-align")
endif()
find_package(PythonLibs 3 REQUIRED)
find_package(PythonInterp 3 REQUIRED)
set(Python3_FIND_FRAMEWORK "LAST")
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)

# Ccache
find_program(CCACHE_PROGRAM ccache)
Expand All @@ -49,12 +49,14 @@ add_library(spacy SHARED
)
install(TARGETS spacy LIBRARY DESTINATION lib)
set_target_properties(spacy PROPERTIES LINK_FLAGS "-fPIC")
target_include_directories(spacy PUBLIC ${PYTHON_INCLUDE_DIRS}
target_include_directories(spacy PUBLIC ${Python3_INCLUDE_DIRS}
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src>")
target_link_libraries(spacy ${PYTHON_LIBRARIES})
target_link_libraries(spacy ${Python3_LIBRARIES})
target_compile_definitions(spacy PRIVATE PYTHON_EXECUTABLE="${Python3_EXECUTABLE}")
message("PYTHON_EXECUTABLE=\"${Python3_EXECUTABLE}\"")

add_library(spacy-hdr INTERFACE)
target_include_directories(spacy-hdr INTERFACE src ${PYTHON_INCLUDE_DIRS})
target_include_directories(spacy-hdr INTERFACE src ${Python3_INCLUDE_DIRS})

# Headers
install(FILES
Expand Down Expand Up @@ -96,8 +98,9 @@ if(SPACYCPP_BUILD_TESTS)

add_executable(test_header_${target} tests/test_${target}.cpp)
target_compile_definitions(test_header_${target} PRIVATE SPACY_HEADER_ONLY=1)
target_link_libraries(test_header_${target} ${PYTHON_LIBRARIES})
target_include_directories(test_header_${target} PRIVATE ${PYTHON_INCLUDE_DIRS})
target_link_libraries(test_header_${target} ${Python3_LIBRARIES})
target_include_directories(test_header_${target} PRIVATE ${Python3_INCLUDE_DIRS})
target_compile_definitions(test_header_${target} PRIVATE PYTHON_EXECUTABLE="${Python3_EXECUTABLE}")
add_test(test_header_${target} "${PROJECT_BINARY_DIR}/test_header_${target}")
endmacro()

Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Spacy-cpp
=========

| **Linux** |
|-----------|
| [![Linux](https://github.com/d99kris/spacy-cpp/workflows/Linux/badge.svg?branch=master)](https://github.com/d99kris/spacy-cpp/actions?query=workflow%3ALinux) |
| **Linux** | **Mac** |
|-----------|---------|
| [![Linux](https://github.com/d99kris/spacy-cpp/workflows/Linux/badge.svg?branch=master)](https://github.com/d99kris/spacy-cpp/actions?query=workflow%3ALinux) | [![macOS](https://github.com/d99kris/spacy-cpp/workflows/macOS/badge.svg?branch=master)](https://github.com/d99kris/spacy-cpp/actions?query=workflow%3AmacOS) |

Spacy-cpp is a C++ wrapper library for the NLP library
[spaCy](https://spacy.io/). This project is not affiliated with spaCy, it is
Expand Down Expand Up @@ -42,6 +42,7 @@ Supported Platforms
===================
Spacy-cpp is implemented using C++11 with the intention of being portable.
Current version has been tested on:
- macOS Ventura 13.1
- Ubuntu 22.04 LTS


Expand All @@ -50,6 +51,17 @@ Pre-requisites
Spacy-cpp requires python development library, pip, spaCy and typically a
spaCy model.

macOS
-----
Install build dependencies:

brew install cmake python

Install spaCy and an English model:

pip3 install -U spacy
python3 -m spacy download en_core_web_sm

Ubuntu
------
Install build dependencies:
Expand Down
6 changes: 3 additions & 3 deletions examples/cmake-add-subdirectory-hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ cmake_minimum_required(VERSION 3.11 FATAL_ERROR)
project(cmake-addsubdirectory)
set(CMAKE_CXX_STANDARD 11)

find_package(PythonLibs 3 REQUIRED)
find_package(PythonInterp 3 REQUIRED)
set(Python3_FIND_FRAMEWORK "LAST")
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)

add_subdirectory(spacy-cpp)

add_executable(exprog1 src/exprog1.cpp)
target_link_libraries(exprog1 PUBLIC spacy-hdr ${PYTHON_LIBRARIES})
target_link_libraries(exprog1 PUBLIC spacy-hdr ${Python3_LIBRARIES})
7 changes: 7 additions & 0 deletions examples/python-spacy-usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp(u"This is a sentence.")
for token in doc:
print(token.text + " [" + token.pos_ + "]")
14 changes: 12 additions & 2 deletions src/spacy/spacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ namespace Spacy
{
if (m_spacy.get() == nullptr)
{
throw std::runtime_error("No module named spacy. Try: pip3 install -U spacy");
#ifdef PYTHON_EXECUTABLE
const std::string interpreter = PYTHON_EXECUTABLE;
#else
const std::string interpreter = "python3";
#endif
throw std::runtime_error("No module named spacy. Try: " + interpreter + " -m pip install -U spacy");
}
}

Expand All @@ -41,8 +46,13 @@ namespace Spacy
PyObjectPtr nlp(Python::call_method<PyObjectPtr>(m_spacy, "load", args));
if (nlp.get() == nullptr)
{
#ifdef PYTHON_EXECUTABLE
const std::string interpreter = PYTHON_EXECUTABLE;
#else
const std::string interpreter = "python3";
#endif
throw std::runtime_error("Can't find model '" + p_model + "'. "
"Try: python3 -m spacy download " + p_model);
"Try: " + interpreter + " -m spacy download " + p_model);
}
return nlp;
}
Expand Down

0 comments on commit 2b7c497

Please sign in to comment.