Skip to content

Instantly share code, notes, and snippets.

@magsol
Last active May 24, 2017 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magsol/1a915e70712c4329156264c7af7aab4a to your computer and use it in GitHub Desktop.
Save magsol/1a915e70712c4329156264c7af7aab4a to your computer and use it in GitHub Desktop.
Simple script to edit the OpenCV conda-forge feedstock's build script to enable CUDA support.
#!/opt/python/bin/python
# Installs a GPU-enabled OpenCV from the conda-forge feedstock.
# Assumes you already have Python 3 installed and the conda-forge channel on.
import argparse
import os.path
import subprocess
import sys
# These are the options we want to make sure are set to the values listed here.
# - If they already exist in the build.sh script but are not set to the value
# listed below, they are changed to that value.
# - If they do not exist, they are appended with the given value.
OPTIONS = (
('DBUILD_TIFF', 1),
('DBUILD_PNG', 1),
('DBUILD_JPEG', 1),
('DWITH_CUDA', 1),
('DWITH_CUBLAS', 1),
('DCUDA_GENERATION', "Auto"),
('DINSTALL_C_EXAMPLES', 1),
('DWITH_FFMPEG', 1),
)
def pad_kv(option, value, arg_length):
"""
Helper function which builds an argument line with the specified key-value
pairing, inserting correct padding where needed.
"""
padding = arg_length - 5 - 2
kv = "{}={}".format(option, value)
rhs = ("{:<" + str(padding) + "}\\\n").format(kv) # This is the -2
lhs = " -" # This is the -5
line = "{}{}".format(lhs, rhs)
if len(line) != arg_length:
print("ERROR: Line length {} is not the specified arg length {}".format(len(line), arg_length), file = sys.stderr)
return line
def insert(contents, to_insert, start_idx, end_idx):
"""
Helper function which splits the original content, inserts the new
content, and glues everything back together.
"""
before = contents[:start_idx]
after = contents[end_idx:]
retval = "{}{}{}".format(before, to_insert, after)
return retval
if __name__ == "__main__":
# Pull in all the build.sh recipe contents.
with open("build.sh", "r") as f:
contents = f.read()
# First, and arguably most importantly, find the newline corresponding to
# the *last* compile option given. This is where new options will be
# inserted if they don't exist.
last_option = contents.rfind("-D")
insertion_index = contents.find("\n", last_option)
idx = last_option
while contents[idx] != "\n": idx -= 1
arg_length = insertion_index - idx
# Loop through all the options--if it exists in the buildfile's contents,
# make sure the corresponding value is correct. If it doesn't exist,
# add it with the correct value.
for option, value in OPTIONS:
index = contents.find(option)
if index >= 0: # Option found!
# Check what the current value of the option is.
str_val = str(value)
# Chop out the line with this config option.
prev_newline = contents.find("\n", index - arg_length)
next_newline = contents.find("\n", index)
current_line = contents[prev_newline + 1:next_newline - 2].strip()
_, current_value = current_line.split("=")
# Are they the same?
if current_value != str_val:
updated = pad_kv(option, value, arg_length)
contents = insert(contents, updated, prev_newline + 1, next_newline + 1)
else: # Option not found! Insert it.
# Create the new line to insert.
to_add = pad_kv(option, value, arg_length)
# Insert the new line, and update the insertion index.
start_index = insertion_index + 1
contents = insert(contents, to_add, start_index, start_index)
insertion_index += arg_length
# In theory--all done!
with open("build.sh", "w") as f:
f.write(contents)
#!/bin/bash
# Installs a GPU-enabled OpenCV from the conda-forge feedstock.
# Assumes you already have Python 3 installed and the conda-forge channel on.
FEEDSTOCK_DIR=feedstock-directory
NUMPY_VER=1.12.1
# Pull down the opencv-feedstock recipe.
git clone https://github.com/conda-forge/opencv-feedstock/ ${FEEDSTOCK_DIR}
mv opencv-cuda.py ${FEEDSTOCK_DIR}/recipe && cd ${FEEDSTOCK_DIR}/recipe
python opencv-cuda.py
# Move to the parent directory and build the feedstock.
conda build --numpy ${NUMPY_VER} recipe
# Finally, to install, run:
# conda install --use-local opencv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment