# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This file is used to build paddle python binding package.
# It will be invoked by Makefile that generated by COMAKE

from setuptools import setup, Extension

import numpy as np
import api.paddle_ld_flags
import platform
import os

system = platform.system().lower()

is_osx = (system == 'darwin')
is_win = (system == 'windows')
is_lin = (system == 'linux')


# The extra links will passed from COMAKE
#   because generate paddle LDFLAGS is too complicated to do in setup.py
#   it just read COMAKE generated LDFLAGS.
extra_comps = []
extra_links = []
obj = api.paddle_ld_flags.PaddleLDFlag()
extra_comps = obj.c_flag()
ldflags = obj.ldflag_str()
if ldflags is not None:
  extra_links.extend(ldflags.split(" "))

try:
  with open('.py_paddle_extra_link_flags', 'r') as f:
    for line in f:
      extra_links += line.split()
except:
  pass

if is_lin == True:
    extra_links = ["-Xlinker", '-start-group'] + extra_links + ["-Xlinker", "-end-group"]
elif is_osx == True:
    os.environ["ARCHFLAGS"] = "-arch x86_64"
    extra_links = ["-Wl,-all_load"] + extra_links

include_dirs = [np.get_include(), "../"]    # include numpy and paddle.

os.environ["CC"] = "@CMAKE_C_COMPILER@"
os.environ["CXX"] = "@CMAKE_CXX_COMPILER@"

setup(name="py_paddle",
  version="@PADDLE_VERSION@",
  ext_modules=[
    Extension('py_paddle._swig_paddle',      # Build SWIG Extension.
       ['Paddle_wrap.cxx'],
       language = "c++",
       include_dirs = include_dirs,
       extra_link_args = extra_links,
       extra_compile_args = extra_comps
    )
  ],
  packages=['py_paddle'],
  include_dirs = include_dirs,
  install_requires = [
    'numpy>=1.8.0',      # The numpy is required.
    'protobuf>=3.0.0'    # The paddle protobuf version
  ],
)
