From e380b41a8e67a18400af73a990cd56647842936c Mon Sep 17 00:00:00 2001
From: Andrey Volk <andywolk@gmail.com>
Date: Wed, 10 Jan 2018 23:30:24 +0300
Subject: [PATCH] FS-10876: [Build-System] Fix build in Visual Studio 2017 and
 Windows SDK 10.

---
 .../libsofia-sip-ua/bnf/sofia-sip/bnf.h        |  9 +++++----
 libs/sofia-sip/libsofia-sip-ua/msg/msg_mime.c  | 18 +++++++++---------
 libs/win32/libx264/vsyasm.props                |  1 +
 libs/win32/libx264/vsyasm.targets              |  8 ++++----
 libs/win32/v8/build-v8.bat                     |  2 ++
 src/switch_apr.c                               | 12 ++++++++++--
 src/switch_estimators.c                        |  3 +--
 src/switch_rtp.c                               |  1 -
 w32/Library/FreeSwitchCore.2015.vcxproj        |  5 +++++
 9 files changed, 37 insertions(+), 22 deletions(-)

diff --git a/libs/sofia-sip/libsofia-sip-ua/bnf/sofia-sip/bnf.h b/libs/sofia-sip/libsofia-sip-ua/bnf/sofia-sip/bnf.h
index 1c9b191e35..f048b9bff4 100644
--- a/libs/sofia-sip/libsofia-sip-ua/bnf/sofia-sip/bnf.h
+++ b/libs/sofia-sip/libsofia-sip-ua/bnf/sofia-sip/bnf.h
@@ -52,15 +52,16 @@ SOFIA_BEGIN_DECLS
 /** Horizontal tab */
 #define HT      "\t"
 /** Carriage return */
-#define CR      "\r"
+/*  CR conflicts with Windows SDK 10, so it is now _CR */
+#define _CR      "\r"
 /** Line feed */
 #define LF      "\n"
 /** Line-ending characters */
-#define CRLF     CR LF
+#define CRLF     _CR LF
 /** Whitespace */
 #define WS       SP HT
 /** Linear whitespace */
-#define LWS      SP HT CR LF
+#define LWS      SP HT _CR LF
 /** Lower-case alphabetic characters */
 #define LOALPHA "abcdefghijklmnopqrstuvwxyz"
 /** Upper-case alphabetic characters */
@@ -160,7 +161,7 @@ enum {
 SOFIAPUBVAR unsigned char const _bnf_table[256];
 
 /** Get number of characters before CRLF */
-#define span_non_crlf(s) strcspn(s, CR LF)
+#define span_non_crlf(s) strcspn(s, _CR LF)
 
 /** Get number of characters before whitespace */
 #define span_non_ws(s) strcspn(s, WS)
diff --git a/libs/sofia-sip/libsofia-sip-ua/msg/msg_mime.c b/libs/sofia-sip/libsofia-sip-ua/msg/msg_mime.c
index e889ceb633..6adf89b22c 100644
--- a/libs/sofia-sip/libsofia-sip-ua/msg/msg_mime.c
+++ b/libs/sofia-sip/libsofia-sip-ua/msg/msg_mime.c
@@ -272,7 +272,7 @@ msg_multipart_boundary(su_home_t *home, char const *b)
   if (!b || !(boundary = su_alloc(home, 2 + 2 + strlen(b) + 2 + 1)))
     return NULL;
 
-  strcpy(boundary, CR LF "--");
+  strcpy(boundary, _CR LF "--");
 
   if (b[0] == '"') /* " See http://bugzilla.gnome.org/show_bug.cgi?id=134216 */
 
@@ -281,7 +281,7 @@ msg_multipart_boundary(su_home_t *home, char const *b)
     strcpy(boundary + 4, b);
 
 
-  strcat(boundary + 4, CR LF);
+  strcat(boundary + 4, _CR LF);
 
   return boundary;
 }
@@ -321,9 +321,9 @@ msg_multipart_search_boundary(su_home_t *home, char const *p, size_t len)
     if (m > 2 && crlf) {
       boundary = su_alloc(home, 2 + m + 2 + 1);
       if (boundary) {
-	memcpy(boundary, CR LF, 2);
+	memcpy(boundary, _CR LF, 2);
 	memcpy(boundary + 2, p, m);
-	strcpy(boundary + m + 2, CR LF);
+	strcpy(boundary + m + 2, _CR LF);
       }
       return boundary;
     }
@@ -342,9 +342,9 @@ msg_multipart_search_boundary(su_home_t *home, char const *p, size_t len)
     if (m > 2 && crlf) {
       boundary = su_alloc(home, 2 + m + 2 + 1);
       if (boundary) {
-	memcpy(boundary, CR LF, 2);
+	memcpy(boundary, _CR LF, 2);
 	memcpy(boundary + 2, p + 1, m);
-	strcpy(boundary + 2 + m, CR LF);
+	strcpy(boundary + 2 + m, _CR LF);
       }
       return boundary;
     }
@@ -589,7 +589,7 @@ int msg_multipart_complete(su_home_t *home,
       b = mp->mp_data;
       m = mp->mp_len;
 
-      if (strncmp(b, CR LF "--", 4) == 0)
+      if (strncmp(b, _CR LF "--", 4) == 0)
 	b += 4, m -= 4;
       else if (strncmp(b, "--", 2) == 0)
 	b += 2, m -= 2;
@@ -638,7 +638,7 @@ int msg_multipart_complete(su_home_t *home,
 
     if (mp->mp_next == NULL) {
       if (!mp->mp_close_delim)
-	mp->mp_close_delim = msg_payload_format(home, "%.*s--" CR LF,
+	mp->mp_close_delim = msg_payload_format(home, "%.*s--" _CR LF,
 						(int)m, boundary);
       if (!mp->mp_close_delim)
 	return -1;
@@ -658,7 +658,7 @@ int msg_multipart_complete(su_home_t *home,
     mp->mp_common->h_len = mp->mp_len;
 
     if (!mp->mp_separator)
-      if (!(mp->mp_separator = msg_separator_make(home, CR LF)))
+      if (!(mp->mp_separator = msg_separator_make(home, _CR LF)))
 	return -1;
 
     if (mp->mp_multipart) {
diff --git a/libs/win32/libx264/vsyasm.props b/libs/win32/libx264/vsyasm.props
index 9efa05172f..14f48a9a66 100644
--- a/libs/win32/libx264/vsyasm.props
+++ b/libs/win32/libx264/vsyasm.props
@@ -16,6 +16,7 @@
     <YASM>
       <Debug>False</Debug>
       <ObjectFile>$(IntDir)</ObjectFile>
+      <ObjectFileName>$(IntDir)\$(Filename).asm.obj</ObjectFileName>
       <PreProc>0</PreProc>
       <Parser>0</Parser>
       <CommandLineTemplate>$(YasmPath)libs\vsyasm.exe -Xvc -f $(Platform) [AllOptions] [AdditionalOptions] [Inputs]</CommandLineTemplate>
diff --git a/libs/win32/libx264/vsyasm.targets b/libs/win32/libx264/vsyasm.targets
index 3c084119b1..5a57d425d2 100644
--- a/libs/win32/libx264/vsyasm.targets
+++ b/libs/win32/libx264/vsyasm.targets
@@ -20,7 +20,7 @@
     AfterTargets="$(YASMAfterTargets)"
     Condition="'@(YASM)' != ''"
     DependsOnTargets="$(YASMDependsOn);ComputeYASMOutput"
-    Outputs="@(YASM->'%(ObjectFile)')"
+    Outputs="@(YASM->'%(ObjectFileName)')"
     Inputs="@(YASM);%(YASM.AdditionalDependencies);$(MSBuildProjectFile)">
     <ItemGroup
       Condition="'@(SelectedFiles)' != ''">
@@ -30,8 +30,8 @@
     </ItemGroup>
     <ItemGroup>
       <YASM_tlog
-        Include="%(YASM.ObjectFile)"
-        Condition="'%(YASM.ObjectFile)' != '' and '%(YASM.ExcludedFromBuild)' != 'true'">
+        Include="%(YASM.ObjectFileName)"
+        Condition="'%(YASM.ObjectFileName)' != '' and '%(YASM.ExcludedFromBuild)' != 'true'">
         <Source>@(YASM, '|')</Source>
       </YASM_tlog>
     </ItemGroup>
@@ -50,7 +50,7 @@
       IncludePaths="%(YASM.IncludePaths)"
       Defines="%(YASM.Defines)"
       UnDefines="%(YASM.UnDefines)"
-      ObjectFile="%(YASM.ObjectFile)"
+      ObjectFile="%(YASM.ObjectFileName)"
       ListFile="%(YASM.ListFile)"
       MapFile="%(YASM.MapFile)"
       ErrorFile="%(YASM.ErrorFile)"
diff --git a/libs/win32/v8/build-v8.bat b/libs/win32/v8/build-v8.bat
index adcc81e02e..24268ba1b8 100644
--- a/libs/win32/v8/build-v8.bat
+++ b/libs/win32/v8/build-v8.bat
@@ -29,6 +29,8 @@ SET GYPFLAGS="-Dv8_use_external_startup_data=0"
 
 CALL .\third_party\python_26\setup_env.bat
 
+SET GYP_MSVS_VERSION=2015
+
 IF "%VisualStudioVersion%" == "11.0" (
 	REM SET VS_VERSION=-Gmsvs_version=2012
         SET GYP_MSVS_VERSION=2012
diff --git a/src/switch_apr.c b/src/switch_apr.c
index 85edb040d0..f6a8d56ea6 100644
--- a/src/switch_apr.c
+++ b/src/switch_apr.c
@@ -59,6 +59,14 @@
 #include <apr_fnmatch.h>
 #include <apr_tables.h>
 
+#ifdef WIN32
+#include "apr_arch_networkio.h"
+/* Missing socket symbols */
+#ifndef SOL_TCP
+#define SOL_TCP IPPROTO_TCP
+#endif
+#endif
+
 /* apr_vformatter_buff_t definition*/
 #include <apr_lib.h>
 
@@ -832,7 +840,7 @@ SWITCH_DECLARE(switch_status_t) switch_socket_opt_set(switch_socket_t *sock, int
 		int r = -10;
 
 #if defined(TCP_KEEPIDLE)
-		r = setsockopt(jsock->client_socket, SOL_TCP, TCP_KEEPIDLE, (void *)&on, sizeof(on));
+		r = setsockopt(sock->socketdes, SOL_TCP, TCP_KEEPIDLE, (void *)&on, sizeof(on));
 #endif
 		if (r == -10) {
 			return SWITCH_STATUS_NOTIMPL;
@@ -846,7 +854,7 @@ SWITCH_DECLARE(switch_status_t) switch_socket_opt_set(switch_socket_t *sock, int
 		int r = -10;
 
 #if defined(TCP_KEEPINTVL)
-		r = setsockopt(jsock->client_socket, SOL_TCP, TCP_KEEPINTVL, (void *)&on, sizeof(on));
+		r = setsockopt(sock->socketdes, SOL_TCP, TCP_KEEPINTVL, (void *)&on, sizeof(on));
 #endif
 
 		if (r == -10) {
diff --git a/src/switch_estimators.c b/src/switch_estimators.c
index 37a7d930df..86d7bfd35c 100644
--- a/src/switch_estimators.c
+++ b/src/switch_estimators.c
@@ -29,9 +29,8 @@
  *
  */
 
-#include <switch_estimators.h>
-
 #include <switch.h>
+
 #ifndef _MSC_VER
 #include <switch_private.h>
 #endif
diff --git a/src/switch_rtp.c b/src/switch_rtp.c
index 15d7e4cc99..afbd01a24e 100644
--- a/src/switch_rtp.c
+++ b/src/switch_rtp.c
@@ -48,7 +48,6 @@
 #include <srtp_priv.h>
 #include <switch_ssl.h>
 #include <switch_jitterbuffer.h>
-#include <switch_estimators.h>
 
 //#define DEBUG_TS_ROLLOVER
 //#define TS_ROLLOVER_START 4294951295
diff --git a/w32/Library/FreeSwitchCore.2015.vcxproj b/w32/Library/FreeSwitchCore.2015.vcxproj
index aa3bff3223..62e2684aee 100644
--- a/w32/Library/FreeSwitchCore.2015.vcxproj
+++ b/w32/Library/FreeSwitchCore.2015.vcxproj
@@ -106,6 +106,11 @@
     <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
     <RunCodeAnalysis>false</RunCodeAnalysis>
   </PropertyGroup>
+  <ItemDefinitionGroup>
+    <ClCompile>
+        <AdditionalIncludeDirectories>..\..\libs\apr\include\arch\win32;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+    </ClCompile>
+  </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <BuildLog />
     <PreBuildEvent>