"""
To upload a new version:
1. make clean
2. git tag a new version: git tag v1.x.x
3. python setup.py sdist
4. python setup.py sdist register upload

Note in binary files (`data_files`) from `setup()` below:

- When you do `python setup.py install` it will place the
binary files (`word2vec`, `word2phrase`, ...) in this directory: `$(pwd)/bin`
So you have to export that to the `$PATH`
- When you do pip install it will place them in the correct
`{{ environtment }}/bin` directory so users dont need to do that.

"""

from distutils.core import setup
from setuptools import find_packages

import os
import sys
import subprocess

import versioneer

SOURCES_DIR = 'word2vec-c'
BIN_DIR = 'bin'
if not os.path.exists(BIN_DIR):
    os.makedirs(BIN_DIR)

def compile_c(source, target):
    CC = 'gcc'
    CFLAGS = ('-lm -pthread -O3 -Wall -march=native -funroll-loops '
              '-Wno-unused-result')
    if sys.platform == 'darwin':
        CFLAGS += ' -I/usr/include/malloc'

    source_path = os.path.join(SOURCES_DIR, source)
    target_path = os.path.join(BIN_DIR, target)
    command = [CC, source_path, '-o', target_path]
    command.extend(CFLAGS.split(' '))
    print(' '.join(command))
    return_code = subprocess.call(command)

    if return_code > 0:
        exit(return_code)

compile_c('word2vec.c', 'word2vec')
compile_c('word2phrase.c', 'word2phrase')
compile_c('distance.c', 'word2vec-distance')
compile_c('word-analogy.c', 'word2vec-word-analogy')
compile_c('compute-accuracy.c', 'word2vec-compute-accuracy')
compile_c('word2vec-sentence2vec.c', 'word2vec-doc2vec')

with open('requirements.txt') as f:
    required = f.read().splitlines()

setup(
    name='word2vec',
    version=versioneer.get_version(),
    cmdclass=versioneer.get_cmdclass(),
    author='Daniel Rodriguez',
    author_email='df.rodriguez143@gmail.com',
    url='https://github.com/danielfrg/word2vec',
    description='Wrapper for Google word2vec',
    license='Apache License Version 2.0, January 2004',
    packages=find_packages(),
    data_files=[('bin', ['bin/word2vec', 'bin/word2phrase',
                         'bin/word2vec-distance', 'bin/word2vec-word-analogy',
                         'bin/word2vec-compute-accuracy',
                         'bin/word2vec-doc2vec'])],
    install_requires=required
)

@@ -395,16 +395,17 @@
  you do 
+%60
 pip inst
@@ -407,16 +407,26 @@
  install
+ word2vec%60
  it will
@@ -801,20 +801,29 @@
 = 'gcc'%0A
+%0A
     
+DEFAULT_
 CFLAGS =
@@ -956,16 +956,24 @@
         
+DEFAULT_
 CFLAGS +
@@ -998,16 +998,70 @@
 /malloc'
+%0A    CFLAGS = os.environ.get('CFLAGS', DEFAULT_CFLAGS)
 %0A%0A    so
@@ -1254,16 +1254,40 @@
  print('
+Compilation command:', '
  '.join(

Use os.environ[CFLAGS]