Skip to content

Commit

Permalink
Fix bugs in finding Tcl on Mac
Browse files Browse the repository at this point in the history
* brew-installed Tcl header files are under `headers/tcl-tk`, not `headers`.
* Fix bug where non-existent Tcl directory could be ignored and the message
"Found Tcl" output, even though it really wasn't.
* Reorganize Tcl-finding function for Mac to make it clearer
  • Loading branch information
garfieldnate committed Jul 26, 2023
1 parent 9530289 commit 8cffd84
Showing 1 changed file with 45 additions and 28 deletions.
73 changes: 45 additions & 28 deletions build_support/tcl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import subprocess
import sys
from typing import Optional

NO_TCL_MSG = "Tcl cannot be built: no Tcl found"

Expand Down Expand Up @@ -33,7 +34,7 @@ def is_valid(self):
return True, ""


def __get_tcl_from_local_dir_mac(env, local_compiled_dir=None) -> TclInstallInfo:
def __get_tcl_from_local_dir_mac(env, local_compiled_dir=None) -> Optional[TclInstallInfo]:
if not local_compiled_dir:
return None

Expand All @@ -47,43 +48,59 @@ def __get_tcl_from_local_dir_mac(env, local_compiled_dir=None) -> TclInstallInfo
)
valid, msg = install_info.is_valid()
if not valid:
print(f"{env['INDENT']}Tcl not found in directory: {msg}")
print(f"{env['INDENT']}Tcl not found in directory {local_compiled_dir}: {msg}")
return None

return install_info

def __get_brew_tcl_install_info_mac(env) -> Optional[TclInstallInfo]:
try:
brew_installed_dir = (
subprocess.check_output(["brew", "--prefix", "tcl-tk"]).decode().strip()
)
except subprocess.CalledProcessError:
print(f"{env['INDENT']}Tcl not brew-installed: {msg}")
return None

def __get_tcl_install_info_mac(env, local_compiled_dir=None) -> TclInstallInfo:
install_info = __get_tcl_from_local_dir_mac(env, local_compiled_dir)
if not install_info:
# brew-installed?
try:
brew_installed_dir = (
subprocess.check_output(["brew", "--prefix", "tcl-tk"]).decode().strip()
)
except subprocess.CalledProcessError:
print(f"{env['INDENT']}Tcl not brew-installed")
install_info = __get_tcl_from_local_dir_mac(env, brew_installed_dir)
home_dir = Path(brew_installed_dir)
install_info = TclInstallInfo(
home=home_dir,
lib_dir=home_dir / "lib",
include_dir=home_dir / "include" / "tcl-tk",
dyn_lib_name="libtcl8.6.dylib",
include_lib_name="tcl8.6",
)
valid, msg = install_info.is_valid()
if not valid:
print(f"{env['INDENT']}Brew-installed Tcl could not be loaded: {msg}")
return None

if not install_info:
# Otherwise, try using the system-installed Tcl
tcl_home = Path("/Library/Frameworks/Tcl.framework/Versions/Current")
install_info = TclInstallInfo(
home=tcl_home,
lib_dir=tcl_home,
include_dir=tcl_home / "Headers",
dyn_lib_name="Tcl",
include_lib_name="Tcl",
using_framework=True,
)
valid, msg = install_info.is_valid()
if valid:
return install_info
print(f"{env['INDENT']}System Tcl not found: {msg}")
return install_info


def __get_system_tcl_install_info_mac(env) -> Optional[TclInstallInfo]:
tcl_home = Path("/Library/Frameworks/Tcl.framework/Versions/Current")
install_info = TclInstallInfo(
home=tcl_home,
lib_dir=tcl_home,
include_dir=tcl_home / "Headers",
dyn_lib_name="Tcl",
include_lib_name="Tcl",
using_framework=True,
)
valid, msg = install_info.is_valid()
if not valid:
print(f"{env['INDENT']}System Tcl not found: {msg}")
return None
return install_info


def __get_tcl_install_info_mac(env, local_compiled_dir=None) -> Optional[TclInstallInfo]:
return __get_tcl_from_local_dir_mac(env, local_compiled_dir) or \
__get_brew_tcl_install_info_mac(env) or \
__get_system_tcl_install_info_mac(env)


def __append_tcl_compile_flags(env, install_info):
env.Append(CXXFLAGS=["-I" + str(install_info.include_dir.absolute())])
env.Append(CPPPATH=[str(install_info.include_dir.absolute())])
Expand Down

0 comments on commit 8cffd84

Please sign in to comment.